Can you set a class instance's attributes to zero by setting the instance to zero?

Terry Hancock hancock at anansispaceworks.com
Sat Nov 19 13:58:59 EST 2005


On 19 Nov 2005 05:29:07 -0800
"Gerard Flanagan" <grflanagan at yahoo.co.uk> wrote:
> If I have the Vector class below, is there a means by
> which I can have the following behaviour
> 
> 
> >>>A = Vector(1, 2)
> >>>print A
> (1, 2)
> >>>A = 0
> >>>print A
> (0, 0)

As has already been mentioned, "A = 0" rebinds the name "A"
to the object "0" so there's no way it can mutate the object
A.

However, you could create an object "Origin" to use as a
vector zero:

Origin = Vector(0,0)

Or if you want to be shorter, more poetic, and make future
maintainers curse you, you can call it "O":

O = Vector(0,0)

;-)

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list