Question about classes

Steven Bethard steven.bethard at gmail.com
Mon Nov 22 13:34:31 EST 2004


Ben wrote:
> On Mon, 22 Nov 2004 06:39:53 +0000, Steven Bethard wrote:
> 
>> >>> class C(object):
>>...     def __init__(self, x):
>>...         if x != 0:
>>...         	self.x = x
>>...         else:
>>...         	self.x = 5
>>...         	
>> >>> c = C(0)
>> >>> c.x
>>5
>> >>> c = C(8)
>> >>> c.x
>>8
> 
> 
> 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:

 >>> class C(object):
...     def __init__(self, x=5):
...         self.x = x
...
 >>> c = C()
 >>> c.x
5
 >>> c = C(8)
 >>> c.x
8
 >>> c = C(0)
 >>> c.x
0

Steve



More information about the Python-list mailing list