[FRPythoneers] Base Object in Python

Sean Reifschneider jafo at tummy.com
Wed Jan 23 16:06:06 MST 2002


On Wed, Jan 23, 2002 at 02:17:51PM -0800, Keith Hellman wrote:
>How do I create the most generic of generic objects in python?  I realize
>it can be as simple as 

Python 2.2 has an "object" type.  This is probably a pretty good base
object if you want no fluff...  It's used in Python 2.2 as the base for
creating "new-style classes".  To make a new-style class, you simply
inherit from an existing new-style class.  In 2.2, "object" is the base you
use when creating a new new-style class.

For example:

   >>> print object
   <type 'object'>
   >>> s = object()
   >>> print dir(s)
   ['__class__', '__delattr__', '__getattribute__', '__hash__', '__init__',
   '__new__', '__reduce__', '__repr__', '__setattr__', '__str__']
   >>> print s
   <object object at 0x812f948>

The problem is that it restricts setting attributes on it:

   >>> s.x = 1
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   AttributeError: 'object' object has no attribute 'x'

So, you may still have to do:

   >>> class nclass(object): pass
   ... 
   >>> sn = nclass()
   >>> sn.x = 1

Though, if you just want to define attributes on an object, why don't you
just use a dictionary?

If you want to know more about new-style classes, well, I guess you should
have been at Monday's FRPythoneer's meeting, eh?  ;-)  Check out the 2.2
page from www.python.org -- both AMK's report on changes and a
tutorial discuss these changes.

Sean
-- 
 Give me a Kaypro 64 and dial-tone and I can do *ANYTHING*!
                 -- Steven, _The_Score_
Sean Reifschneider, Inimitably Superfluous <jafo at tummy.com>
tummy.com - Linux Consulting since 1995. Qmail, KRUD, Firewalls, Python



More information about the FRPythoneers mailing list