473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
6 1382
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian Munroe | last post by:
I am just starting to learn the OO side of Python scripting, and I am a little confused on the following. Take the following example class: >>> class rectangle(object): z = 1 def...
2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
4
by: bruno modulix | last post by:
Hi How can I make a *class* attribute read-only ? The answer must be pretty obvious but I just can't find it (it's late and I've spent all day on metaclasses, descriptors and the like, which,...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
0
by: Gerard Flanagan | last post by:
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
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
3
by: antred | last post by:
Hello everyone, While working on a program I encountered a situation where I'd construct a largish data structure (a tree) from parsing a host of files and would end up having to throw away...
4
by: Basilisk96 | last post by:
This topic is difficult to describe in one subject sentence... Has anyone come across the application of the simple statement "if (object1's attributes meet some conditions) then (set object2's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.