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

Chris Angelico rosuav at gmail.com
Sun Aug 3 08:56:37 EDT 2014


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.

Python 3.4 on Windows (32-bit):
>>> import sys
>>> class Data: pass
>>> sys.getsizeof(Data())
32
>>> sys.getsizeof(object())
8

Even before you add a single attribute, the object is four times the
size it needs to be. When you need a sentinel (for a function's
default argument, for instance), you don't need any attributes on it -
all you need is some object whose identity can be tested. And the
excess would be multiplied by a fairly large number of objects in the
system (it's not just object() that doesn't take attributes). If you
want a bag of attributes, get a bag of attributes, don't expect
object() to be it :) 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.

ChrisA



More information about the Python-list mailing list