Hello,
is there a assignement operator, that i can overwrite?
class MyInt:
def __init__(self, val):
assert(isinstan ce(val, int))
self._val = val
a = MyInt(10)
# Here i need to overwrite the assignement operator
a = 12
Thanks
Alexander 17 1479
Alexander Eisenhuth wrote:
Hello,
is there a assignement operator, that i can overwrite?
You can't overwrite assignment operator, but you can
overwrite methods of numeric objects: http://docs.python.org/ref/numeric-types.html
HTH,
Rob
Alexander Eisenhuth a écrit :
Hello,
is there a assignement operator, that i can overwrite?
Adding to Simon Brunning reply (assignment is a statement).
class MyInt:
def __init__(self, val):
assert(isinstan ce(val, int))
self._val = val
a = MyInt(10)
# Here i need to overwrite the assignement operator
a = 12
Here you bind the 12 (Python int value) to name 'a', then 'a' has the
int type, not your MyInt (which value has been lost).
You may define a 'set' method, and write:
a = MyInt(10)
a.set(12)
And, if a is a member of another class, you may define an accessor for
that 'a' member in that class, which automatically call your set method
when giving an int value.
b.a = MyInt(10)
b.a = 12 ---b.a.set(12)
A+
Laurent.
On Tue, 17 Oct 2006 15:50:47 +0200, Alexander Eisenhuth wrote:
Hello,
is there a assignement operator, that i can overwrite?
No.
We were just discussing the reasons why Python will not and can not have
an assignment operator just a few days ago. Check the archives for more
details.
class MyInt:
def __init__(self, val):
assert(isinstan ce(val, int))
isinstance() considered harmful: http://www.canonical.org/~kragen/isinstance/
self._val = val
Seems kind of pointless. What does MyInt do that ordinary ints don't?
Apart from slow your program down and require extra programming effort.
a = MyInt(10)
# Here i need to overwrite the assignement operator a = 12
Can't happen. "a" is just a name, not an object with methods that can be
called. "a" can be bound to anything, not just MyInt instances. Objects
like MyInt(10) can have no name, one name or many names:
mylist = [0, MyInt(10), 20, 30] # MyInt instance has no name
x = MyInt(10) # MyInt instance has one name
x = y = z = MyInt(10) # MyInt instance has many names
--
Steven.
On Oct 17, 8:50 am, Alexander Eisenhuth <newsu...@staco m-software.de>
wrote:
Hello,
is there a assignement operator, that i can overwrite?
class MyInt:
def __init__(self, val):
assert(isinstan ce(val, int))
self._val = val
a = MyInt(10)
# Here i need to overwrite the assignement operator
a = 12
Thanks
Alexander
I believe the property function is what you are looking for. e.g.
class MyClass:
def __init__(self, val):
self.setval(val )
def getval(self):
return self._val
def setval(self, val):
assert(isinstan ce(val, int))
self._val = val
_val = property(self.g etval, self.setval)
--
Jerry
Steven D'Aprano wrote:
On Tue, 17 Oct 2006 15:50:47 +0200, Alexander Eisenhuth wrote:
>Hello,
is there a assignement operator, that i can overwrite?
No.
We were just discussing the reasons why Python will not and can not have
an assignment operator just a few days ago. Check the archives for more
details.
>class MyInt: def __init__(self, val): assert(isinstan ce(val, int))
isinstance() considered harmful:
Trying to convert val to an int would probably be better indeed:
class MyInt(object):
def __init__(self, val):
self.val = int(val)
My 2 cents
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jerry wrote:
(snip)
I believe the property function is what you are looking for.
It is not.
e.g.
class MyClass:
Descriptors don't work fine with old-style classes. Should be:
class MyClass(object) :
def __init__(self, val):
self.setval(val )
def getval(self):
return self._val
def setval(self, val):
assert(isinstan ce(val, int))
self._val = val
_val = property(self.g etval, self.setval)
NameError : self is not defined.
Should be :
_val = property(getval , setval)
but then - since setval() now calls _vals.__set__() , which itself calls
setval(), you have a nice infinite recursion (well, almost infinite -
hopefully, Python takes care of it).
May I kindly suggest that you learn more about properties and test your
code before posting ?-)
Anyway, even with the following correct code, this won't solve the OP's
question:
class MyClass(object) :
def __init__(self, val):
self.val = val
def _getval(self):
return self._val
def _setval(self, val):
self._val = int(val)
val = property(_getva l, _setval)
m = MyClass(42)
m
=<__main__.MyCl ass object at 0x2ae5eaa00410>
m.val
=42
m = 42
m
=42
type(m)
=<type 'int'>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Wow, thanks a lot for your quick answers.
That assignement is no operator, but a statemant is a pity, but indeed I came
foward with overwritten methods for numeric types
Regards
Alexander
class MyClass:Descrip tors don't work fine with old-style classes.
Interesting, I have used this construct before in Python 2.4.3 and not
run into the recursion problem you talk about. Also, it has worked
fine for me. Perhaps you can post a link to your source so that I
could study it and understand what circumstances my solution works and
what the recommended construct actually is.
May I kindly suggest that you learn more about properties and test your
code before posting ?-)
I did test this on Python 2.4.3 in Mac OS X 10.4 and it worked fine.
--
Jerry This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Harald Massa |
last post by:
Old, very old informatical problem: I want to "print" grouped data with
head information, that is:
eingabe=
shall give: ( Braces are not important...)
'Stuttgart', '70197'
--data-- ('Fernsehturm', '20')
|
by: yuraukar |
last post by:
I am trying to create a garbage collection class in C++ to collect
instances of a specific class (so no general gc). The approach is to
use smart pointers and a mark and a simple sweep gc.
smartptr<B> pB;
pB = new B(args);
The constructor of B has been adjusted to register new instances with
the garbage collector. The constructor of smartptr has also been
adjusted to register new instances of smart pointers with the garbage
|
by: sushant |
last post by:
hi all,
whats the difference between assignement and initialisation in C.
is assignement related with scanf() and initialisation with (int x=10;)
sushant
|
by: Jim Langston |
last post by:
I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is exceptionally
ugly. For example, in my operator<< override:
template<typename T>
CMySQLTable& operator<<(const T& ClassTable)
{
/**/
}
|
by: mscava |
last post by:
I'm building DataManager<T>. A class where shared data will be stored.
One of things useful to implement is garbage collection. But it still
gives me this error:
stl_algo.h:1076: error: non-static const member `const std::string
DataMapPair::first', can't use default assignment operator
Here's my code:
typedef std::map< std::string, std::pair< bool, CountedPtr<T >
| |
by: sublimanized |
last post by:
Hello all ... here is my problem. I just got the book "Teach Yourself C
++ in 21 Days"
Setup:
Fedora Core 6 - i386
GCC 4.1.1
Again, I am a complete newcomer to C++, and this is one of the first
examples out of the book. I want to know why exactly this doesn't
clean compile, and how would i fix it?
|
by: mihai |
last post by:
I work at an application witch has embeded python.
We have an python type X.
# a != b
a = b # at this point both variables have the same value
b = select_other()
|
by: mosfet |
last post by:
Hi,
In one my class I would like to give access to one of my buffer so I
have declared something like this :
vector<char>& RespData = pWebResp->get_RawBuffer();
The problem is caller can also write this :
vector<charRespData = pWebResp->get_RawBuffer();
|
by: John Doe |
last post by:
Hi,
I am trying to replace the use of the Windows CString class by a
compatible one (CStdString) using std::string , the problem is I cannot
do the following thing :
A)
CString strFullPath;
CStdString& str = strFullPath;
|
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...
|
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,...
| |
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |