Can we create an_object = object() and add attribute like for a class?
- From: Pierre Rouleau <prouleau@xxxxxxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 15:32:16 -0400
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
.
- Follow-Ups:
- Re: Can we create an_object = object() and add attribute like for a class?
- From: Alex Martelli
- Re: Can we create an_object = object() and add attribute like for a class?
- Prev by Date: Re: urllib.urlopen() with pages that requires cookies.
- Next by Date: Re: (was Re: Xah's Edu Corner: Criticism vs Constructive Criticism)
- Previous by thread: Re: [wwwsearch-general] Re: Urllib2 / add_password method
- Next by thread: Re: Can we create an_object = object() and add attribute like for a class?
- Index(es):
Relevant Pages
|