By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hello

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)

If there is such a means, will it still work with the __slots__
attribution uncommented?

Thanks

class Vector(object):

#__slots__ = ('x', 'y')

def __init__(self, a=0, b=0 ):
self.x = a
self.y = b

def __repr__(self):
return '(%s, %s)' % (self.x, self.y)

Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gerard Flanagan wrote:
Hello

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)

If there is such a means, will it still work with the __slots__
attribution uncommented?


No, you can't. The reason is that python doesn't have an assignment
operator as e.g. C++ has. The "=" just binds the name A to some object -
without the object knowing it.

What's wrong with

class A:
def clear():
pass
...

A.clear()

? Alternatively, you could try and abuse one of the seldom used in-place
operator like __ior__:

A |= 0

But I wouldn't recommend that.

Regards,

Diez
Nov 22 '05 #2

P: n/a
Gerard Flanagan wrote:
Hello

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)

If there is such a means, will it still work with the __slots__
attribution uncommented?


No, you can't. The reason is that python doesn't have an assignment
operator as e.g. C++ has. The "=" just binds the name A to some object -
without the object knowing it.

What's wrong with

class A:
def clear():
pass
...

A.clear()

? Alternatively, you could try and abuse one of the seldom used in-place
operator like __ior__:

A |= 0

But I wouldn't recommend that.

Regards,

Diez
Nov 22 '05 #3

P: n/a
Gerard Flanagan 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


that operation rebinds A; it doesn't affect the Vector instance in any way.

more here:

http://effbot.org/zone/python-objects.htm

</F>

Nov 22 '05 #4

P: n/a
Gerard Flanagan 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


that operation rebinds A; it doesn't affect the Vector instance in any way.

more here:

http://effbot.org/zone/python-objects.htm

</F>

Nov 22 '05 #5

P: n/a
On 19 Nov 2005 05:29:07 -0800
"Gerard Flanagan" <gr********@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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Nov 22 '05 #6

P: n/a
On 19 Nov 2005 05:29:07 -0800
"Gerard Flanagan" <gr********@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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.