class vs function ???

Alan Gauld alan.gauld at btinternet.com
Sat Feb 21 20:44:43 CET 2004


On 21 Feb 2004 08:57:44 -0800, gveda at iitk.ac.in (Gaurav Veda)
wrote:
> What is the difference between a class and a function in Python ???

Pretty much the same as in every other OOP programming language.
Your question is not really about Python but about OO.

To try to answer it I'll start with the big picture answer:

A function is an operation that returns a value. The value may be
None. A function can be used directly by other bits of your
program.

A class is a combination of some data and the functions which
operate on that data all wrapped up as a single item. A class is
a template which is used to create objects. You normally use the
objects in your program not the classes.

You might want to look at the Modular Programming and OOP topics
in my online tutorial for more details.(see the .sig)

Now let's look at your issues:

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

Here we define a function called fun1 which takes no parameters
and returns None as a value (None is the defauilt in Python).
Inside the function is some code which, incidentally, does not
follow good programming practice. 

[It is better to have your functions completely predictable in
effect. Thus values are passed in, those values are not changed
by the function, the function does not print anything, and the
function returns a result. ]

Of course you are simply printing for debug purposes but in
general its bad practice. However this is all aside from your
main issue...


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

This defines a second function called c1 which amongst other
things defines yet another function, fun2, and calls it.
You then call c1.

> class c2:
>     pass

Here we define a class called c2 which does nothing.
And we do not create any objects from it.

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

Here we create a new class called c1 which contains a variable
called c. In the process of defining the class we incidentally
define another function called fun2 and call it. It has nothing
to do with the class as such.

> class c2:
>     pass

And here we define another new class called c2 which does nothing
except replace the reference to the old c2 class which will, in
due time, be garbage collected.

We have not yet created any objects from any of our classes.
We have executed the class definition code which just happens to
do some printing of values and defining and executing of
functions. But the net result is two classes, one of which is
empty and the other contains a single variable and a function
which is effectively unusable.

We can see the variable by typing

print c1.c


> The output of both these codes are precisely the same ! (including the
> error because of 'print c')

That's correct, but just because the visible output is the same
does not mean that they are doing the same thing. Nor even that
the end result is the same.

Lets look at one feature of your program again in more detail:

def fun1():
    c = 12
    print "inside fun1"
    print c

class c1:
    d = 42
    print "defining class c1"
    print d

fun1()
print c
print c1.d

Now we can print the d that is inside c1 but not the c that is
inside fun1. This is because c only exists while fun1 is
executing. But d persists after the class is created because the
class itself persists.

To make c visible after fun1 completes we must declare it global,
as you did, but that in reality global simply creates a value
outside of fun1. (See the namespaces topic in my tutor for
that...)

A class is a container and anything you create within its
definition persists and can be accessed, whereas a function is
just a bit of executable code and any values created inside it
are lost when the function exits.

However the real power of classes is only seen once you create
instances of them - ie objects. But that's for another time.

HTH,

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Python-list mailing list