2

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

void foo();



void foo() 
{

}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Noam
  • 96
  • 1
  • 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. – Martijn Pieters Feb 14 '15 at 08:56
  • No, it is not possible. – Ozgur Vatansever Feb 14 '15 at 08:57
  • 1
    Are you looking for _forward declarations_? Those aren't needed in Python. – Dan D. Feb 14 '15 at 08: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**. – Martijn Pieters Feb 14 '15 at 08:58
  • But first `foo` being set to `None` doesn't imply anything about the next `foo` being set to a function. So I think, it is a bit different than the case we have here. – Ozgur Vatansever Feb 14 '15 at 08:59
  • I am learning Python after a while of programming in c / c++ , and it is nicer to read . what about class functions ? Do I have to implement those in the class declaration as well ? @MartijnPieters – Noam Feb 14 '15 at 09:00
  • @ozgur: nope. And `foo` being bound to a function doesn't mean you cannot later replace that binding with something else. Or delete it. In Python *it doesn't matter*. – Martijn Pieters Feb 14 '15 at 09:01
  • @MartijnPieters I'd thought it like "declare the method signature now" and synthesize its body in some other place, similar to what `@dynamic` does in objective-c. – Ozgur Vatansever Feb 14 '15 at 09:04
  • @Noam: they are still resolved dynamically at runtime. Are you getting any actual errors here? – Martijn Pieters Feb 14 '15 at 09:04
  • 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). – Martijn Pieters Feb 14 '15 at 09:06
  • I am not getting any errors , it works . I was just looking for a more "readable" way to write functions and classes . thanks @MartijnPieters – Noam Feb 14 '15 at 09:06
  • @Noam: You said: "what about class functions?" You can define class methods outside the class, if you really want to. They can even be added to a class after you've already created some instances of it and the existing instances will "inherit" the new methods. It's also possible to add methods to individual instances, but that's a tiny bit more work. However, generally it's best to keep it simple and just define methods inside the class. – PM 2Ring Feb 14 '15 at 10:20

3 Answers3

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
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"
Sigve Karolius
  • 1,356
  • 10
  • 26
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.

Eithos
  • 2,421
  • 13
  • 13