class vs function ???

Scott David Daniels Scott.Daniels at Acm.Org
Sat Feb 21 14:11:02 EST 2004


Gaurav Veda wrote:

> I am a poor mortal who has become terrified of Python.
It's a nice snake, but don't let it wrap itself around you.

> What is the difference between a class and a function in Python ???
Substantial.  However, your code is not testing that.
The thing to remember is that definitions are executed
to create the defined thing.

A function definition creates code and then stores the code as
the name in the def part.

A class definition creates a new "context,", then interprets the
rest of the class definition in that context.  At the end of the
class definition, the class name, superclass list, and all names
defined in context are handed to a metaclass in order to build
a class from that definition, and the class returned by the
metaclass is then stored under the class name.  One of the jobs
of the metaclass is to convert functions defined in the context
into "unbound methods".

Your code, if typed in to an interactive interpreter, would
clue you in that this was happening.

> # Fragment 1 begins 
> 
> a = 1
a is defined
> print a
1 is printed here
> def fun1():
>     global b
>     b = "inside fun1"
>     print b
nothing is printed here, but fun1 is defined.
now you have a and fun1
> fun1()
'inside fun1' is printed here, and b is defined
> def c1():
>     print a
>     print b
>     c = 12
>     def fun2():
>         global d
>         d = "We are dead !!!"
>         print d
>     fun2()
Again, nothing is printed here, c1 is defined.
> c1()
Now you get the output 'We are dead!!!', and d is defined.
> class c2:
>     pass
c2 the class is defined.
> print d
the d produced by c1 above is printed.
> print c
I cannot imagine where c came from (it was local to c1, so
goes away when c2 is exited (and is only available inside c1
and fun2 (because fun2 was defined inside c1).
> 
> # Fragment 1 ends
> # Fragment 2 begins
> a = 1
> print a
> def fun1():
>     global b
>     b = "inside fun1"
>     print b
> fun1()
up to here is all works the same.
> class c1:
>     print a
here 1 is printed
>     print b
here 'inside fun' is printed
>     c = 12
Here a variable named c is created in the context which will become c1
>     def fun2():
>         global d
>         d = "We are dead !!!"
>         print d
Here a function named fun2 is created in the ...
>     fun2()
Here, within the pending context, the normal function fun2 is called.
'We are dead!!!' is printed and the global d is defined.

At the end of c1's definition (because of the outdent below),
a metaclass is handed 'c1', empty superclasses, and a dictionary
containing entries for 'c' and 'fun2'.  Now fun2 is converted from
a function to an unbound method in this process.
you could type 'print c1.c' to see a 12 come out.

> class c2:
>     pass
Another class is defined.

> print d
d was created by the function fun2 when called inside c1's definition.
> print c
c is a class variable on c1.  You could type 'print c1.c'

> Waiting for some 'logical' explanation !!!
I hope this helps.

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list