473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1395
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********@yah oo.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*****@Anansi Spaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Nov 22 '05 #6
On 19 Nov 2005 05:29:07 -0800
"Gerard Flanagan" <gr********@yah oo.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*****@Anansi Spaceworks.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
2482
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 __init__(self): self.x = 2 >>> r = rectangle() >>> print r.z
2
2043
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 Base class (old-style) which defines a default class attribute 'bar' too. I can't modify the source code of this class. Note that Workes does not inherit from Base. - a Derived class which must inherit from Base and is a wrapper around Worker: it...
50
6392
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
4
2597
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, as fun as it is, may have side-effects on intellectual abilities...) *The context:*
166
8698
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
353
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
13717
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
1667
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 parts of my newly built tree if a file turned out to contain invalid data. My first thought was 'Well, you can always make a deep copy of your tree first, then add new data to the copy and revert to the original if you need to.", but as this tree can...
4
1887
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 attributes to certain outcomes)", where "object1" and "object2" are generic objects, and the "conditions" and "outcomes" are dynamic run- time inputs? Typically, logic code for any application out there is hard-coded. I have been working with...
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.