Problem with operator overloading and inheritance in Python

Calvin Spealman ironfroggy at
Tue Sep 19 00:01:34 CEST 2006

On 9/18/06, Edward Waugh <edward_waugh at> wrote:
> I quickly glanced at Guido's paper of new-style classes and I am puzzled
> because the definition of a new-style class looks exactly the same as that
> for the classic classes: class <name>[<optional subclasses>]: <definition of
> the class>.
> How do I define new-style classes?
> - Edward
> ----Original Message Follows----
> From: "Calvin Spealman" <ironfroggy at>
> Reply-To: ironfroggy at
> To: "Edward Waugh" <edward_waugh at>
> Subject: Re: Problem with operator overloading and inheritance in Python
> Date: Mon, 18 Sep 2006 17:06:53 -0400
> On 9/18/06, Edward Waugh <edward_waugh at> wrote:
> >Tried this and it didn't work:
> >
> >C:\Files\Python>
> >Traceback (most recent call last):
> >   File "C:\Files\Python\", line 21, in ?
> >     x += 1
> >   File "C:\Files\Python\", line 7, in __add__
> >     return type(self)( + value)
> >TypeError: instance() argument 1 must be classobj, not int
> >
> >But then in a moment of inspiration I tried self.__class__( +
> >value) and it worked:
> Sorry, I neglected to consider that you might be using old-style
> classes! For any old-style class, type(someInstance) is always the
> instance type, where as new-style classes actually give yuou the class
> itself! So either you need to do what you have done now or learn about
> and move to new-style classes. Usually you can just inherit your base
> classes from object and you wont have any trouble.

They are syntactically the same, but the difference is in some subtle
behaviors related to some lookup rules in various situations, like the
dropping of the instance type. By inheriting from object, the base
new-style class, you cause your class to become a new-style class.
This should all have been covered in anything you read.

# Old-Style
class foo:

# New-Style
class foo(object):

More information about the Python-list mailing list