Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can you set a class instance's attributes to zero by setting the instance to zero?

Reply
Thread Tools

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

 
 
Gerard Flanagan
Guest
Posts: n/a
 
      11-19-2005
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)

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-19-2005
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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-19-2005
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>



 
Reply With Quote
 
Terry Hancock
Guest
Posts: n/a
 
      11-19-2005
On 19 Nov 2005 05:29:07 -0800
"Gerard Flanagan" <(E-Mail Removed)> 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 ((E-Mail Removed))
Anansi Spaceworks http://www.AnansiSpaceworks.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Can you create an instance of a subclass with an existing instance of the base class? Sandra-24 Python 18 04-29-2006 04:01 PM
Dynamically create a class (or class instance) and its attributes? Robert Oschler Python 3 07-27-2004 02:48 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments