http://ibiblio.org/g2swap/byteofpython/read/index.html
http://stackoverflow.com/questions/9606624/use-a-class-before-its-definition-in-django-model
defined before it's usage
http://stackoverflow.com/questions/261599/why-can-i-use-a-function-before-its-defined-in-javascript
http://stackoverflow.com/questions/4069100/why-cant-i-use-a-javascript-function-before-its-definition-inside-a-try-block
http://stackoverflow.com/questions/1590608/is-it-possible-to-forward-declare-a-function-in-python
If you don't like declaring a function before a use and declaring a function after is impossible, what about declaring it in some other module? Technically you still declare it before but it's clean.
You can create a recursion like the following:
def foo():
bar()
def bar():
foo()
(infinite, yes, but it works)
Python's functions are anonymous just like values are anonymous, yet they can be bound to a name. In the above code,
foo()
does not call a function with the name foo, it calls a function that happens to be bound to the name foo
at the point the call is made. It is possible to redeclare foo
somewhere else down the code and bar
will therefore call the new function.
Your 'problem' simply cannot be solved because it's precisely like asking to get a variable which has not been declared.
(actually, it's not really 'anonymous', but I don't know what to call it)
If the call to cmp_configs is inside its own function definition, you should be fine. I'll give an example.
def a():
b() # b() hasn't been defined yet, but that's fine because at this point, we're not
# actually calling it. We're just defining what should happen when a() is called.
a() # This call fails, because b() hasn't been defined yet,
# and thus trying to run a() fails.
def b():
print "hi"
a() # This call succeeds because everything has been defined.
I came here looking for the exact same solution to the question. But I may have come up with a solution. What if instead, you created a handler, then put that handler below all the methods you need to call. Then just use said handler to call your functions.
The handler takes an argument "nameOfMethodToCall" Then uses a bunch of if elifs to call the right method.
I think this would solve our issue.
def foo():
print("foo")
#take input
nextAction=input('What would you like to do next?:')
return nextAction
def bar():
print("bar")
nextAction=input('What would you like to do next?:')
return nextAction
def handler(action):
if(action=="foo"):
nextAction = foo()
elif(action=="bar"):
nextAction = bar()
else:
print("You entered invalid input, defaulting to bar")
nextAction = "bar"
return nextAction
nextAction=input('What would you like to do next?:')
while 1:
nextAction = handler(nextAction)
that seems very unpythonic. Python should handle this sort of stuff all by itself. – Nathan Fellman Aug 4 at 5:47
re-read the accepted answer. Python does not need the function to be defined until you call it, not just use it in a definition. – tcaswell Aug 4 at 6:50
main()
? – Sanjay Manohar Apr 8 at 16:27import your_module
– J.F. Sebastian Jun 19 at 13:41