Question about classes

Ben reply at to-the-newsgroup.com
Mon Nov 22 01:20:18 EST 2004


Consider:

class foo:
    x = 5

def getx(r):
    a = foo()
    if r != 0:
        a.x = r
    return a

a = getx(8)

------------------------------------------------
the above works (unless I typoed it, I'm tired)

this illustrates instatiation of a class with initialization dependent
upon a passed parameter. Seems quite useful in the general sense (in fact,
I have a use for it, which is what led me to this.) But this doesn't work:

class foo(xstart):
    x = 5
    if xstart != 0:
        x = xstart

a = foo(8)

What I am curious about is why not? What am I missing about classes here?
Is the functionality delivered in some other fashion, or must I:

class foo:
    x = 5

a = foo()
a.x = 8

to get the brevity and clarity of...

class foo(xstart):
    x = 5
    if xstart != 0:
        x = xstart

a = foo(8)

Thanks for any input on this.




More information about the Python-list mailing list