Is it possible to declare functions and implement them separately in python ? I mean something like in C :
void foo();
void foo()
{
}
C forward declarations are used to work around dependency problems. Function foo is used by function bar, and foo needs bar to exist before you can declare it:
void bar()
{
if (condition) { foo(); }
}
void foo()
{
if (condition) { bar(); }
}
won't compile because foo hasn't been declared yet; void foo(); is the C spelling for I know what I am doing, compiler, accept that foo will exist later.
There are no such dependency problems in Python, as global names are looked up at runtime; they don't have to yet exist at compile time.
In other words, this just works:
def bar():
if condition: foo()
def foo():
if condition: bar()
because bar and foo are resolved at runtime.
If your script is standalone you can use __name__=='__main__' to circumvent your problem with forward declaration read more.
Note that this is not really an answer to your question but a work around. Consider the following script as an example.
def main():
bar()
def bar():
print "Hello World"
if __name__=="__main__":
main() # can be any function not necessarily "main"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With