Python function parameter as a global variable
Solution 1
The simplest thing would be to return the value, and assign it outside the function:
def my_input(prompt):
#.. blah blah..
return the_value
month = my_input("Please enter the month")
# etc.
Solution 2
Other people are saying something like this:
def input(prompt):
return value
value = input(param1,param2, ...)
And that's what you really want to be doing, but just so you know, you can use globals() for changing global variables:
def input(input_name, prompt):
globals()[input_name] = value
Solution 3
What you want to do is probably a bad practice. Just return input_name
from the input
function.
def input(param1,param2):
return value
value = input(param1,param2, ...)
Comments
-
Tom Kadwill about 4 years
I have written the following function, it takes in a variable
input_name
. The user then inputs some value which is assigned toinput_name
. I want to know the best way to makeinput_name
accessible outside of the function. I know that defining a variable as global, inside a function, means that is can be used outside of the function. However, in this case the variable as actually a parameter of the function so I am not sure how to define it as a global variable. I appreciate any help with this, please find the code in question below:def input(input_name, prompt): while True: data = raw_input(prompt) if data: try: input_name = int(data) except ValueError: print 'Invalid input...' else: if input_name >= 0 and input_name < 100: print 'Congratulations' break input_name = 'Please try again: ' else: print 'Goodbye!' break month = 0 day = 0 year = 0 century = 0 input(month, "Please enter the month (from 1-12, where March is 1 and February is 12): ") input(day, "Please enter the day (from 1-31): ") input(year, "Please enter the year (from 0 - 99, eg. 88 in 1988): ") input(century, "Please enter the century (from 0 - 99, eg. 19 in 1988): ") A = month B = day C = year D = century
-
poke about 12 yearsOn a side note:
input_name = 'Please try again: '
– You probably want to setprompt
there, notinput_name
.
-
-
Karl Knechtel about 12 years+1 The way you get information out of a function is via the return value. I don't understand why so many people are so resistant to such a simple concept.
-
Anorov about 12 years@KarlKnechtel It's not that they're resistant to it. I've found that many Python beginners don't really understand the return statement. Some don't see it as any different from print, because the REPL prints return values by default. See this discussion here, posted by Guido van Rossum: plus.google.com/115212051037621986145/posts/NJnmxZkrE4J I think they see functions more as simple code blocks or labels, and not procedures that return something when given input. That basic programming concept is not taught in most Python tutorials.
-
Tom Kadwill about 12 yearsThanks for the help here. So if I do
return input_name
inside the function then I should be able to sayprint input_name
outside of the function? At present the program is just returning 0 (as I initialized the variables as 0). -
Karl Knechtel about 12 yearsVariables are names for values. A value potentially has different names in different contexts and at different times. In the example here,
the_value
is the name, inside the function, for the value that is returned. It gets assigned tomonth
; thus, outside the function,month
is a name for that value, andprint month
prints the value. There is nothing in this code that is analogous toinput_name
in your code, because part of the suggestion is to get rid of that idea completely. You do not pass a parameter in order to get information back; you get it back via the return value. -
bmello over 7 yearsThanks, Elijaheac. Although it is not what Tom was asking for, your response was very useful for me. Please correct me if I am wrong, but I believe this is a good way to set the value of a module's variable that has the same name as the function argument. I have been doing it by having a different name for the argument and for the module's variable, but will from now on use your suggestion.