Correct type for a simple “bag of attributes” namespace object

Roy Smith roy at panix.com
Sun Aug 3 09:25:53 EDT 2014


In article <mailman.12581.1407070605.18130.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Aug 3, 2014 at 10:40 PM, Roy Smith <roy at panix.com> wrote:
> >  I usually just do:
> >
> > class Data:
> >    pass
> > my_obj = Data()
> >
> > That's all you really need.  It's annoying that you can't just do:
> >
> > my_obj = object()
> >
> > which would be even simpler, because (for reasons I don't understand),
> > you can't create new attributes on my_obj.
> 
> [...]
> The only reason I can think of for expecting a
> basic object to work this way is because of the parallel with
> ECMAScript; it's not a fundamental part of the type hierarchy.

I don't know about that.  I agree that your explanation about object 
size makes sense for why it wasn't built to work that way, but I don't 
think the expectation should be surprising.  When I write:

class Foo(Bar):
   # stuff

I'm saying, "make Foos be just like Bars, except for this stuff".  I can 
do:

>>> class Foo(object):
...     pass
... 
>>> f = Foo()
>>> f.x = 1

in which case, I've said, "make Foos just like objects, except for, oh, 
never mind, there aren't any differences".  But, in reality, the system 
bolted on the ability to have user-defined attributes without telling 
me.  I don't think it's unreasonable to be surprised at that.



More information about the Python-list mailing list