class vs function ???

From: Gaurav Veda (gveda_at_iitk.ac.in)
Date: 02/21/04


Date: 21 Feb 2004 08:57:44 -0800

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 ???
Consider the following code fragments :
# Fragment 1 begins

a = 1
print a
def fun1():
    global b
    b = "inside fun1"
    print b
fun1()
def c1():
    print a
    print b
    c = 12
    def fun2():
        global d
        d = "We are dead !!!"
        print d
    fun2()
c1()
class c2:
    pass
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()
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')

The sample output is :
1
inside fun1
1
inside fun1
We are dead !!!
We are dead !!!
Traceback (most recent call last):
  File "XYZ.py", line 21, in ?
    print c
NameError: name 'c' is not defined

Waiting for some 'logical' explanation !!!

Gaurav



Relevant Pages

  • Re: class vs function ???
    ... > What is the difference between a class and a function in Python ??? ... > def fun1(): ... > def fun2(): ...
    (comp.lang.python)
  • Re: class vs function ???
    ... Your question is not really about Python but about OO. ... Here we define a function called fun1 which takes no parameters ... In the process of defining the class we incidentally ... do some printing of values and defining and executing of ...
    (comp.lang.python)
  • Re: How do I create an array of functions?
    ... def fun1(): return "fun1" ... def fun2(): return "fun2" ... def fun3(): return "fun3" ...
    (comp.lang.python)
  • Re: How do I create an array of functions?
    ... def fun1(): return "fun1" ... def fun2(): return "fun2" ... def fun3(): return "fun3" ...
    (comp.lang.python)
  • Re: Understanding closures
    ... def fun2: ... this is fun2' % method ... local variable 'x' referenced before assignment ...
    (comp.lang.python)