Can we create an_object = object() and add attribute like for a class?

Pierre Rouleau prouleau at impathnetworks.com
Sat Apr 29 15:32:16 EDT 2006


Hi all,

Is there any reason that under Python you cannot instantiate the object 
class and create any attributes like you would be able for a normal class?

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> a = object()
 >>> a.data = 1
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'object' object has no attribute 'data'
 >>>
 >>> class Object:
...     pass
...
 >>> a = Object()
 >>> a.data = 1
 >>> print "a.data = ", a.data
a.data =  1
 >>>
 >>> class Object2(object):
...     pass
...
 >>> b = Object2()
 >>> b.data = 2
 >>> b.data
2

I also tried with Python 2.4.3 with the same results.
Being able to do it would seem a natural way of declaring namespaces.

--
Pierre Rouleau



More information about the Python-list mailing list