Question about classes

B

Ben

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.
 
S

Steven Bethard

Ben said:
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

The parentheses after a class name do not indicate a parameter list;
they indicate the list of base classes. So generally, they must by
classes/types:
.... class C(base):
.... pass
.... return C
....Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in make_class
TypeError: Error when calling the metaclass bases
int() takes at most 2 arguments (3 given)

If you want to set a class or instance variable for an object at
instantiation time, you probably want to supply the __init__ method:
.... x = 5
.... def __init__(self, x):
.... if x != 0:
.... C.x = x
.... 8

It seems strange to me that you want to set a *class* variable here, not
an instance variable, though perhaps you have your reasons. Note that
by doing so, every time you make a new instance, you'll change the x
attribute for all objects:
8

If instead, you intended to set an instance variable, you might write it
like:
.... def __init__(self, x):
.... if x != 0:
.... self.x = x
.... else:
.... self.x = 5
.... 8

Steve
 
S

Steve Holden

Steven said:
The parentheses after a class name do not indicate a parameter list;
they indicate the list of base classes. So generally, they must by
classes/types:

.... class C(base):
.... pass
.... return C
....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in make_class
TypeError: Error when calling the metaclass bases
int() takes at most 2 arguments (3 given)

If you want to set a class or instance variable for an object at
instantiation time, you probably want to supply the __init__ method:

.... x = 5
.... def __init__(self, x):
.... if x != 0:
.... C.x = x
....
8

It seems strange to me that you want to set a *class* variable here, not
an instance variable, though perhaps you have your reasons. Note that
by doing so, every time you make a new instance, you'll change the x
attribute for all objects:

8

If instead, you intended to set an instance variable, you might write it
like:

.... def __init__(self, x):
.... if x != 0:
.... self.x = x
.... else:
.... self.x = 5
....
8

Steve

Don't forget, though, that due to the attribute resolution order, if an
instance doesn't have a particular attribute but the class does then a
reference to the attribute in a method will get the class attribute.

This has been used to implement class defaults in the past.

regards
another Steve
 
B

Ben

If instead, you intended to set an instance variable, you might write it
like:

... def __init__(self, x):
... if x != 0:
... self.x = x
... else:
... self.x = 5
...
8

Steve,

That's exactly what I wanted. Thank you very much. I'll keep the class one
in mind too.

--Ben


--Ben
 
S

Steven Bethard

Ben said:
Steve,

That's exactly what I wanted. Thank you very much. I'll keep the class one
in mind too.

You're welcome. Depending on your goal here, you might also find that
default argument values are helpful:
.... def __init__(self, x=5):
.... self.x = x
....0

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top