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) 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
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
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>
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>
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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,...
|
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
|
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
|
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;
}
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |