2

When I call the function using following way,

method_a()
def method_a():

    print "I am method A"

I got an error from above code,

Traceback (most recent call last):
  File "class1.py", line 1, in <module>
    method_a()
NameError: name 'method_a' is not defined

When I call the function following way,

def method_a():

    print "I am method A"

method_a()

I got correct output without any errors from the above code

I am method A 

What is the difference between above two codes ?. Thanks..

dhana
  • 6,487
  • 4
  • 40
  • 63
  • python is not C :) check this post for some more clarification as well http://stackoverflow.com/questions/1590608/is-it-possible-to-forward-declare-a-function-in-python – Samuele Mattiuzzo Jun 19 '13 at 11:43
  • @SamueleMattiuzzo, Is this allowed in C? – awesoon Jun 19 '13 at 11:44
  • yes it's called prototyping: http://en.wikipedia.org/wiki/Function_prototype#Uses – Samuele Mattiuzzo Jun 19 '13 at 11:51
  • @SamueleMattiuzzo, AFAIK, in Python declaration and definition is the same things. So, code, presented in the question is not allowed in Python, nor C, since function is _declared after_ using. – awesoon Jun 19 '13 at 12:06
  • 1
    @Golgauth, AFAIK, there is no such thing as _prototyping_ in the Python, isn't it? Correct me, please, if I'm wrong. – awesoon Jun 19 '13 at 12:08
  • @soon Sorry I answered to the wrong person : `Is this allowed in C`: No, for the same reason as explained in answer below. sequential... Unless you use prototyping (Samuele Mattiuzzo's link), of course. This doesn't exist in Python. :) – Gauthier Boaglio Jun 19 '13 at 12:12
  • 1
    @soon well "this" precise example, as pointed out by Golgauth isn't allowed. The link points to the correct way of using prototypes. – Samuele Mattiuzzo Jun 19 '13 at 12:25

4 Answers4

10

Python executes the source file from top to bottom. The method is not defined until def method_a(): ... is executed, which creates it.

pts
  • 80,836
  • 20
  • 110
  • 183
2

It works like this:

call_function() #Looks up in the dictionary of all the functions and executes it.

def call_function(): #Adds the function to the dictionary of all functions
    print "a"

Which means that you can't call a function before you define it. But what if you call a function from another function?

def f1():
    f2()

def f2():
    print "a"

Why does this work?

It works because when you define f1, it is not executed, just added, so only when you try to execute it, f2 will be searched inside the dictionary, and in this case it will be there since it is defined right after.

LtWorf
  • 7,286
  • 6
  • 31
  • 45
1

In your case (Python + main thread), the code is executed sequentially :

Sequential code means that it is accessed by a single thread, in the order of appearance of the instructions. This means that a single thread can only do code in a specific order, hence it being sequential. The other thing is concurrent code, multiple threads may access the same code synchronously. The programming needs special care put in to it, as multi-threading can pose security risks and inconsistency risks.

In your example, method_a() must exist (be defined) before you call (use) it, since you are running in the main thread of your program.

Note: Here you have to make the distinction between definition (at interpretation time) and execution (at runtime). Python interprets first, then executes... (See LtWorf's answer).

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
1

in the first case the method is called before it created so the python interpreter thinks it doesn't exist...

but in the second case it has already been created (defined is the pythonic term though) and then been called, so python successfully executes it!

one more thing to understand here is that the python interpreter interprets the code from top to bottom

tenstar
  • 9,816
  • 9
  • 24
  • 45