class vs function ???

Stephen Horne steve at ninereeds.fsnet.co.uk
Sat Feb 21 18:08:08 EST 2004


On 21 Feb 2004 08:57:44 -0800, gveda at iitk.ac.in (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 ???

Lots. However, there are shared ideas which are generally very useful.

Clearly you have spotted that the body source code of a class is
normal code, rather than some alternate 'definition block syntax'.
It's execution builds the class. This is a very simple yet powerful
concept, and very appropriate to a scripting language.

But the similarity in your code snippets is a carefully constructed
illusion. You claim the function 'c1' is equivalent to the class 'c1',
but it is not. The body of function 'c1' only executes because you
called it. The body of class 'c1' executes in order to define the
class - you didn't instantiate it at all.

Being able to use print statements in within the class definition is,
BTW, convenient for debugging, but wouldn't normally be done in
deliverable code. As for calling a just-defined member function within
the definition of a class - well, that's pretty strange. But just
because something is rarely done, it doesn't mean it should be
illegal. At the very least, it could be useful for assertions and
other debug checks.

As for the error with 'c' - of course it's an error in both, and the
same error as in both cases there is no identifier 'c' in scope at the
point of the print statement. The fact that neither block of code
created a suitable identifier is not very strange - there are an
infinite number of ways of writing a block of code that doesn't define
a global 'c' ;-)

However, your two cases are still completely different when you look
at the 'c' identifiers that you did define. In the first case, 'c' is
a local variable in the function - it no longer exists after the call
is completed. In the second case, 'c' is a member variable in the
class - it still exists, but as you did not use the dot notation in
the print statement you are not referencing the member field.


Similarly, I could observe that the following three expressions give
the same result...

>>> 2+2
4
>>> 2*2
4
>>> 2**2
4

I can even get essentially the same errors out of those operators...

>>> class a :
...   pass
...
>>> a+a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'classobj' and
'classobj'
>>> a*a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for *: 'classobj' and
'classobj'
>>> a**a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for ** or pow(): 'classobj' and
'classobj
'

That does not mean, however, that addition, multiplication and raising
to a power are the same thing. These are just carefully selected
special cases of the use and abuse of those operators. Which is ok, if
you enjoy playing with such things ;-)


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list