class vs function ???

Shalabh Chaturvedi shalabh at cafepy.com
Sat Feb 21 15:19:19 EST 2004


Gaurav Veda wrote:

> Hi !
> 
> I am a poor mortal who has become terrified of Python. It seems to
> have thrown all the OO concepts out of the window. Penniless, I ask a
> basic question :
> What is the difference between a class and a function in Python ???

As seen in your code, you have to call the function to execute the code in
it. And as not seen, you can create instances of classes but not of
functions.

At the risk of telling too much, I'll mention two concepts here:

1. Everything is an object. A class, a function, a list, a string, an int
are all objects. This means you can pass functions and classes around like
other objects.

2. Objects are created as and when specified. See comments below.

> Consider the following code fragments :
> # Fragment 1 begins
> 
> a = 1

Here you create a new int object and bind it to the name 'a'.

> print a
> def fun1():
>     global b
>     b = "inside fun1"
>     print b

Here you create a new function object and bind it to name 'fun1'. The def
statement creates a new function object. The code is 'stored' inside the
function object and gets executed later, when you call the function like...

> fun1()
> def c1():
>     print a
>     print b
>     c = 12
>     def fun2():
>         global d
>         d = "We are dead !!!"
>         print d
>     fun2()

Another function got created. Note that the 'def fun2()' statement will not
be executed until c1 is called. And everytime it is called, a new fun2 will
be created (just like a new c is created by 'c = 12'). Also, there is no
way you can call fun2 from outside c1.

> c1()

Another function call.

> class c2:
>     pass

Here you create a new class and bind it to the name 'c2'. The class
statement creates a class. Code inside the class body is 'executed' to
create the attributes of the class. Here you have no code. If you had for
example:

class c2:
    a = 1
    b = 2
    c = a + b

then you'd have a class with three class attributes (c2.a, c2.b and c2.c).
Everything inside the body is executed *once* and all the names are
collected to make the class.

> print d
> print c
> 
> # Fragment 1 ends
> # Fragment 2 begins
> a = 1
> print a
> def fun1():
>     global b
>     b = "inside fun1"
>     print b
> fun1()
> class c1:
>     print a
>     print b
>     c = 12
>     def fun2():
>         global d
>         d = "We are dead !!!"
>         print d
>     fun2()

A class with a body. Note the 'execution' of the 'def fun2()' statement
creates a function object which ends up in the class (c1.fun2). It is
executed only once, fun2 will never be created again. Calling fun2() in the
body isn't really much use (neither are the print statements).

If you call a class (which you never do), you create an instance of a class
(which is yet another object). Then you can call the functions defined in
the class from the instance. The tutorial might help you here - to use
classes like classes in any other language, as a template for creating
instances and holding common methods.

> class c2:
>     pass
> 
> print d
> print c
> 
> # Fragment 2 ends
> 
> The output of both these codes are precisely the same ! (including the
> error because of 'print c')

Python keeps all OO concepts intact. The implementation may be somewhat
different from some other languages, but a class behaves like a class and a
function behaves like a function.

Python is mostly easy. If some things are confusing, I suggest you initially
'use as directed', i.e. go through the tutorial and follow other examples
of how it is used.

--
Shalabh



More information about the Python-list mailing list