Can we create an_object = object() and add attribute like for a class?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Pierre Rouleau <prouleau@impathnetworks.com>
Date: Sat Apr 29 2006 - 21:32:16 CEST

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
Received on Mon May 1 00:44:51 2006