473,881 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator overloading

Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.

Here is my try:
>>class myint(int):
def __pow__(self, value):
return self.__add__(va lue)
>>a = myint(3)
a ** 3
6

OK, it works. Now I try different way to achieve the same result but
without much luck:
>>class myint(int):
pass
>>myint.__pow __ = myint.__add__
or:
>>class myint(int):
__pow__ = int.__add__

or:
>>class myint(int):
pass
>>a.__pow__ = a.__add__
but for every try the result was the same:
>>a = myint(3)
a ** 3
27

Why it doesn't works ?

Apr 4 '07 #1
10 2405
looping wrote:
Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.

Here is my try:
>class myint(int):

def __pow__(self, value):
return self.__add__(va lue)
>a = myint(3)
a ** 3

6

OK, it works. Now I try different way to achieve the same result but
without much luck:
>class myint(int):
pass
>myint.__pow_ _ = myint.__add__

or:
>class myint(int):

__pow__ = int.__add__

or:
>class myint(int):
pass
>a.__pow__ = a.__add__

but for every try the result was the same:>>a = myint(3)
>a ** 3
27

Why it doesn't works ?
This looks like a bug in Python. It works for all the other
operators:
>>class MyInt(int):
.... __sub__ = int.__add__
.... __mul__ = int.__add__
.... __div__ = int.__add__
.... __truediv__ = int.__add__
.... __floordiv__ = int.__add__
.... __mod__ = int.__add__
.... __lshift__ = int.__add__
.... __rshift__ = int.__add__
.... __and__ = int.__add__
.... __xor__ = int.__add__
.... __or__ = int.__add__
.... __pow__ = int.__add__
....
>>i = MyInt(42)
i + 3
45
>>i - 3
45
>>i * 3
45
>>i / 3
45
>>i // 3
45
>>i % 3
45
>>i << 3
45
>>i >3
45
>>i & 3
45
>>i ^ 3
45
>>i | 3
45
>>i ** 3
74088

You should submit a bug report to the bug tracker:

http://sourceforge.net/bugs/?group_id=5470

Ziga

Apr 4 '07 #2
On Apr 4, 3:36 am, "looping" <kad...@gmail.c omwrote:
Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.

Here is my try:>>class myint(int):

def __pow__(self, value):
return self.__add__(va lue)
>a = myint(3)
a ** 3

6

OK, it works. Now I try different way to achieve the same result but
without much luck:
>class myint(int):
pass
>myint.__pow_ _ = myint.__add__

or:>>class myint(int):

__pow__ = int.__add__

or:
>class myint(int):
pass
>a.__pow__ = a.__add__

but for every try the result was the same:>>a = myint(3)
>a ** 3

27

Why it doesn't works ?
Look at the output of the following code:

class myint(int):
pass

print myint.__pow__
print myint.__add__

-----output:-----
<slot wrapper '__pow__' of 'int' objects>
<slot wrapper '__add__' of 'int' objects>

That shows that the names '__pow__' and '__add__' are __slots__.
According to Python in a Nutshell(p.102) , a name that is a slot can
only be "bound"(i.e . assigned to) inside the class body. Any later
attempt to assign something to a name that is a slot has no effect.
In your case, I think that means that the only place you can assign
something to an int slot is inside the int class.

As an alternative, you could override __pow__(). Something like this:

class myint(int):
def __pow__(self, num):
return super(myint, self).__add__(n um)

n = myint(3)
print n**3

---output:---
6

That doesn't attempt to reassign something to the slot int.__pow__;
rather it creates a new __pow__ name that hides int.__pow__.

Apr 4 '07 #3
On Apr 4, 12:41 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
On Apr 4, 3:36 am, "looping" <kad...@gmail.c omwrote:
Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.
Here is my try:>>class myint(int):
def __pow__(self, value):
return self.__add__(va lue)
>>a = myint(3)
>>a ** 3
6
OK, it works. Now I try different way to achieve the same result but
without much luck:
>>class myint(int):
pass
>>myint.__pow __ = myint.__add__
or:>>class myint(int):
__pow__ = int.__add__
or:
>>class myint(int):
pass
>>a.__pow__ = a.__add__
but for every try the result was the same:>>a = myint(3)
>>a ** 3
27
Why it doesn't works ?

Look at the output of the following code:

class myint(int):
pass

print myint.__pow__
print myint.__add__

-----output:-----
<slot wrapper '__pow__' of 'int' objects>
<slot wrapper '__add__' of 'int' objects>

That shows that the names '__pow__' and '__add__' are __slots__.
According to Python in a Nutshell(p.102) , a name that is a slot can
only be "bound"(i.e . assigned to) inside the class body. Any later
attempt to assign something to a name that is a slot has no effect.
In your case, I think that means that the only place you can assign
something to an int slot is inside the int class.

As an alternative, you could override __pow__(). Something like this:

class myint(int):
def __pow__(self, num):
return super(myint, self).__add__(n um)

n = myint(3)
print n**3

---output:---
6

That doesn't attempt to reassign something to the slot int.__pow__;
rather it creates a new __pow__ name that hides int.__pow__.
Hmmm...after reading Ziga's post and doing some more testing, I don't
think __slots__ has anything to do with it.
Apr 4 '07 #4
On Apr 4, 12:41 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
According to Python in a Nutshell(p.102) , a name that is a slot can
only be "bound"(i.e . assigned to) inside the class body.
Upon closer reading, it actually says that the name "__slots__" has to
be bound inside the class body for the slot restrictions to take
effect.

Apr 4 '07 #5

"Ziga Seilnacht" <zi************ @gmail.comwrote in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
| This looks like a bug in Python. It works for all the other
| operators:
|
| >>class MyInt(int):
| ... __sub__ = int.__add__
| ... __mul__ = int.__add__
| ... __div__ = int.__add__
| ... __truediv__ = int.__add__
| ... __floordiv__ = int.__add__
| ... __mod__ = int.__add__
| ... __lshift__ = int.__add__
| ... __rshift__ = int.__add__
| ... __and__ = int.__add__
| ... __xor__ = int.__add__
| ... __or__ = int.__add__
| ... __pow__ = int.__add__
| ...
| >>i = MyInt(42)
| >>i + 3
| 45
| >>i - 3
| 45
| >>i * 3
| 45
| >>i / 3
| 45
| >>i // 3
| 45
| >>i % 3
| 45
| >>i << 3
| 45
| >>i >3
| 45
| >>i & 3
| 45
| >>i ^ 3
| 45
| >>i | 3
| 45
| >>i ** 3
| 74088
|
| You should submit a bug report to the bug tracker:
|
| http://sourceforge.net/bugs/?group_id=5470

Nice test. I thought maybe __pow__ might be different in not having a
reverse form, but indeed, int has an __rpow__ method.

Before submitting, make sure that this does not work in 2.5, and then say
so in the bug report. In fact, copy the version/system info that the
interactive interpreter puts up when it starts.

tjr



Apr 4 '07 #6
On Apr 4, 4:55 pm, "Terry Reedy" <tjre...@udel.e duwrote:
"Ziga Seilnacht" <ziga.seilna... @gmail.comwrote in message

news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
| This looks like a bug in Python. It works for all the other
| operators:
[SNIP]
| >>i ** 3
| 74088
|
| You should submit a bug report to the bug tracker:
|
|http://sourceforge.net/bugs/?group_id=5470

Nice test. I thought maybe __pow__ might be different in not having a
reverse form, but indeed, int has an __rpow__ method.

Before submitting, make sure that this does not work in 2.5, and then say
so in the bug report. In fact, copy the version/system info that the
interactive interpreter puts up when it starts.
FWIW:
Python 2.5 (r25:51908, Jan 21 2007, 03:10:25)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on HOSTNAME_REDACT ED
Type "help", "copyright" , "credits" or "license" for more information.
>>class MyInt(int):
.... __pow__ = int.__add__
....
>>i=MyInt(42)
i**3
74088

Apr 4 '07 #7

<sj*******@yaho o.comwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.com.. .
| FWIW:
| Python 2.5 (r25:51908, Jan 21 2007, 03:10:25)
| [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on HOSTNAME_REDACT ED
| Type "help", "copyright" , "credits" or "license" for more information.
| >>class MyInt(int):
| ... __pow__ = int.__add__
| ...
| >>i=MyInt(42)
| >>i**3
| 74088

Thanks. Bug report submitted
http://sourceforge.net/tracker/index...70&atid=105470
or
http://python.org/sf/1694663

Terry Jan Reedy

Apr 4 '07 #8
looping wrote:
Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.

Here is my try:
>>>class myint(int):
def __pow__(self, value):
return self.__add__(va lue)
>>>a = myint(3)
a ** 3
6

OK, it works. Now I try different way to achieve the same result but
without much luck:
>>>class myint(int):
pass
>>>myint.__pow_ _ = myint.__add__

or:
>>>class myint(int):
__pow__ = int.__add__

or:
>>>class myint(int):
pass
>>>a.__pow__ = a.__add__

but for every try the result was the same:
>>>a = myint(3)
a ** 3
27

Why it doesn't works ?
This may, or may not be a bug, depending on ones interpretation.
int.__pow__ is a ternary operation while int.__add__ is binary. For a
Python function, like def __pow__(self, ...), the interpreter can't
properly check if the method takes the correct number of arguments, so
accepts it without question. But since int.__add__ is a slot wrapper the
interpreter can tell, and chokes. So it just falls back to using
int.__pow__.

Now for a more detailed explanation. Each Python type, and class, has a
vtable of C function pointers for all the operations the interpreter
understands. A type only defines functions for those operations it
supports. The remaining pointer are null. These functions are known as
slot functions, a slot being a C structure field in a Python object
instance (Types are instances too!). The int type has slot functions for
binary + and ternary ** for instance, but none for container [].

To make the slot mechanism visible to Python, allowing operator
overloading, generic wrapper slot functions and slot wrapper methods are
used. When a special method, such as __add__ or __pow__, is added to a
class, either in the class declaration or later by attribute assignment,
the corresponding slot is set to point to a generic wrapper that will
actually call the special method. When a builin type, like int, has a
type specific slot function for a particular operation, a corresponding
slot wrapper is added to the type that can call the slot function. So
not only can one define addition for a class by having an __add__ method
(via generic wrappers), but one can call the __add__ method on an int
(via slot wrappers).

Now for the specific case of myint. Not all slot functions are created
equal. The binary + slot function takes two arguments, the ternary **
slot function three. So int's corresponding __add__ and __pow__ slot
wrapper methods also differ. When subclassing int all goes well:

class myint(int):
pass

The myint class inherits int's slot functions for arithmetic, and those
functions can be called by int's slot wrappers, such as __add__ and
__pow__. But the following is unexpected:

myint.__pow__ = myint.__add__

Here __pow__ calls a slot function taking three arguments, __add__ calls
one taking two. What follows is probably unplanned, no exception is
raised. myint's ** slot function is not replaced with a generic wrapper
that will call the __pow__ method. Instead myint keeps the slot function
inherited from int. So myint may have a __pow__ method that adds, but it
is never called when doing a **.
Curiously C PyPy does what was expected:
>pypy-c.exe
Python 2.4.1 (pypy 1.0.0 build 41438) on win32
Type "help", "copyright" , "credits" or "license" for more information.
>>>class MyInt(int):
..... pass
.....
>>>MyInt.__pow_ _ = MyInt.__add__
mi=MyInt(1 2)
mi**2
14
>>>>

Lenard Lindstrom
Apr 5 '07 #9
sj*******@yahoo .com wrote:
On Apr 4, 4:55 pm, "Terry Reedy" <tjre...@udel.e duwrote:
>"Ziga Seilnacht" <ziga.seilna... @gmail.comwrote in message

news:11******* *************** @e65g2000hsc.go oglegroups.com. ..
| This looks like a bug in Python. It works for all the other
| operators:
[SNIP]
>| >>i ** 3
| 74088
|
| You should submit a bug report to the bug tracker:
|
|http://sourceforge.net/bugs/?group_id=5470

Nice test. I thought maybe __pow__ might be different in not having a
reverse form, but indeed, int has an __rpow__ method.

Before submitting, make sure that this does not work in 2.5, and then say
so in the bug report. In fact, copy the version/system info that the
interactive interpreter puts up when it starts.

FWIW:
Python 2.5 (r25:51908, Jan 21 2007, 03:10:25)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on HOSTNAME_REDACT ED
Type "help", "copyright" , "credits" or "license" for more information.
>>>class MyInt(int):
... __pow__ = int.__add__
...
>>>i=MyInt(42 )
i**3
74088
Interestingly, Jython doesn't suffer from this drawback, which I suspect
is due to special-casing of the __pow__ operator that was discussed
quite recently on python-dev without anybody noticing this aspect of things.

C:\jython2.2b1> jython
[...]
>>class S(int):
.... pass
....
>>S.__pow__ = S.__add__
s = S(12)
s ** 2
14
>>>
Note that it still doesn't work to override the *instance's* method.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
Apr 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2628
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
34
6485
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
16
3102
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2070
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
67
8683
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
3
2306
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
3635
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
3
3291
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
9
3525
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
8
2979
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9776
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
11098
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...
1
10814
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10401
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
9552
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
7953
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...
0
7109
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
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...
3
3223
bsmnconsultancy
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...

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.