newbie OO question

lukasz.ciesnik at gmail.com lukasz.ciesnik at gmail.com
Thu Apr 20 16:13:25 EDT 2006


Jeremy Winters napisal(a):
> class SegmentValue:
> 	def
> __init__(self,seg=[0,0,0,0,0,0],value=0,description=""):
> 		self.segment=seg
> 		self.value=value
> 		self.description=description
>
> #that's my class! note the default of a 6 item list
> for the seg parameter... which is then bound(?) to the
> segment attribute of the instance...
>
> now here's the session I just did
>
> >>> from SegmentValue import *
> >>> a=SegmentValue([1,2,3,4,5,6],22,"segment a")
> >>> b=SegmentValue()
> >>> c=SegmentValue()
> >>> b.segment[0]=1
> >>> b.segment
> [1, 0, 0, 0, 0, 0]
> >>> c.segment
> [1, 0, 0, 0, 0, 0]
> >>> a.segment
> [1, 2, 3, 4, 5, 6]
>
> so... what I'm seeing here is that when I explicitly
> set seg during instantiation... it creates a unique
> sequence that can be manipulated without affecting
> other instances... but if I instantiate using the
> default... it seems to refer back to the default when
> maniuplating the segment attribute.
>
> in a continuation of the session...
>
> >>> d=SegmentValue()
> >>> d.segment
> [1, 0, 0, 0, 0, 0]
>
> ...you'll note that any new instances now refer to the
> *modified default sequence*.
>
> what is going on here?  how do I instantiate without
> explicitly defining a new sequence each time? or is
> this even possible?
>
> thanks in advance,
> jeremy
>
Read the doc:
http://docs.python.org/tut/node6.html#SECTION006710000000000000000
Important warning: The default value is evaluated only once.




More information about the Python-list mailing list