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



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
.



Relevant Pages

  • Re: pickle broken: cant handle NaN or Infinity under win32
    ... >> Win32 when it always works fine under Linux: ... Python deals with all sorts of problems for which there is no ... > there is not a good portable way to do the pickle / unpickle. ... handles Win32 and glibc (e.g. Linux). ...
    (comp.lang.python)
  • Re: Win32 Binary-only for 2.3.x?
    ... get just the binaries for, say, 2.3.5 for win32. ... that doesn't want it wrapped in an installer. ... python binary" I get about nine thousand results pointing to Mark ...
    (comp.lang.python)
  • [ANN] IPython 0.6.11 is out.
    ... I'm glad to announce the release of IPython 0.6.11. ... should work correctly for both Python 2.3 and 2.4. ... Win32 users who grabbed the 0.6.11 which I put in testing last week ...
    (comp.lang.python)
  • Re: Shed Skin - Does it break any Python assumptions?
    ... time.sleep now works on Win32. ... Given Python's highly dynamic nature it's unclear to me how Shed Skin could ... a more static language than Python. ... Can one module tweak another module's attributes? ...
    (comp.lang.python)
  • Open existing Word document, modify and save.
    ... New to Python and new to Win32. ... so this might seem like one of those "can you do my homework" ... an existing document and modify certain parts of it...and then it ...
    (comp.lang.python)