[Tutor] integers are also objects?

dman dsh8290@rit.edu
Mon, 3 Dec 2001 10:49:11 -0500


On Mon, Dec 03, 2001 at 01:40:23AM -0800, Danny Yoo wrote:
| On Mon, 3 Dec 2001, Remco Gerlich wrote:
| 
| > On  0, karthik Guru <karthikg@aztec.soft.net> wrote:
| > > thanks a lot for the information.
| > > since integer are objects, do integers also inherit from some
| > > class?? rather are integers instance of some class?
| > 
| > No. They're objects, but not class instances. They're types.
| > 
| > This sort of thing is being changed in the very latest Pythons, and I
| > haven't followed the recent development, but as far as I know it's
| > still true. Soon there may be an Integer class that does this, but I
| > don't know the details.
| 
| There are details online about the type/class unification that's part of
| the upcoming 2.2 release:
| 
|    http://www.python.org/2.2/descrintro.html
| 
| The __future__ looks quite exciting (and frightening) indeed.  *grin*

I think it looks cool.  Some of the new things are neat and I think
will work very naturally.

As far as integers go, in 2.2b2 they are instances of the "int"
class (or is it a metaclass, anyways it's the new-style classes) which
inherits from the "object" class.  You can create a subclass of it :

class MyInt( int ) :
    # override something here,
    # or add something here,
    # or both
    pass


Due to the potential for backwards-compatibility issues, old style
classes are the default, eg :

class AClass : pass

and new-style classes are used if the class inherits from a new-style
class, eg :

class AnotherClass( object ) : pass

the difference is irrelevant if you don't make use of any of the
advanced features.  The difference can be significant if you get into
dynamic attribute access (__getattribute__ and __setattribute__) and
the lookup order for inherited members/methods.

-D

-- 

If we confess our sins, He is faithful and just and will forgive us our
sins and purify us from all unrighteousness.
        I John 1:9