2

Is it possible to declare functions and implement them separately in python ? I mean something like in C :

void foo();



void foo() 
{

}
12
  • Why would you need to? Python functions are first class objects, their names just names like any other. You can set the name to None at first, but because Python looks up global names at runtime you don't need to. Commented Feb 14, 2015 at 8:56
  • No, it is not possible. Commented Feb 14, 2015 at 8:57
  • 1
    Are you looking for forward declarations? Those aren't needed in Python. Commented Feb 14, 2015 at 8:57
  • 2
    @ozgur: it is possible but not needed. The equivalent would be to use foo = None. Or foo = lambda *args, **kw: None. But the syntax in C is used for a problem that doesn't exist in Python. Commented Feb 14, 2015 at 8:58
  • 1
    @ozgur: In C? Yes. In Python? Names are bound to objects. Functions are just one type of object that happen to be callable. It doesn't matter what their signature is at that point. You can create other callable types too (just give your class a __call__ method). Commented Feb 14, 2015 at 9:06

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

0

I'm not familiar with C. Judging by the comments you've already gotten, seems like you're trying to solve a problem that doesn't exist in Python.

The closest thing I can think of for what you're asking is this:

def foo(): pass

This is used sometimes for testing purposes when laying out a class, for example, and you wish it to run smoothly even if you haven't written the code for a particular function yet.

However, there is another use for it. If you're trying to create the template method pattern, callback functions declared in the baseclass could take the following form :

def callback(): pass

These methods could then be implemented in subclasses (optionally), as opposed to abstract methods, which must be implemented.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.