object().__dict__

Amirouche Boubekki amirouche.boubekki at gmail.com
Wed Apr 23 09:48:32 EDT 2014


2014-04-23 8:11 GMT+02:00 Cameron Simpson <cs at zip.com.au>:

> On 23Apr2014 09:39, Pavel Volkov <sailor at lists.xtsubasa.org> wrote:
>
>> There are some basics about Python objects I don't understand.
>> Consider this snippet:
>>
>>  class X: pass
>>>>>
>>>> ...
>>
>>>  x = X()
>>>>> dir(x)
>>>>>
>>>> ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
>> '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
>> '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
>> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
>> '__str__', '__subclasshook__', '__weakref__']
>>
>>>  x.foo = 11
>>>>>
>>>>
>> And now I want to make a simple object in a shorter way, without
>> declaring X class:
>>
>
If you don't want to go through the usual syntax that defines classes you
can use the equivalent code using type to dynamically create a class:

MyClassObject = type('MyClassObject', (object,), dict())

Mind the fact that MyClassObject is a specific kind of object: a class, a
priori, not harmful in anyway


>
>>  y = object()
>>>>> dir(y)
>>>>>
>>>> ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
>> '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
>> '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
>> '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
>> '__subclasshook__']
>>
>>>  y.foo = 12
>>>>>
>>>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> AttributeError: 'object' object has no attribute 'foo'
>>
>> The attribute list is different now and there's no __dict__ and the
>> object does not accept new attributes.
>> Please explain what's going on.
>>
>
> The base "object" class has a fixed set of attributes; you can't add more.
>

Just like any other builtin type: int, float etc... which also means you
can't add method to them dynamically.

Mind the fact that an instance of object is still useful to implement
efficient SENTINEL objects


>
> Almost every other class lets you add attributes, but the price for that
> is that it is slightly in memory footprint and slower to access.
>

class defined in Python except if you define a __slots__


>
> Look up the "__slots__" dunder var in the Python doco index:
>
>   https://docs.python.org/3/glossary.html#term-slots
>
> You'll see it as a (rarely used, mostly discouraged) way to force a fixed
> set of attributes onto a class. As with object, this brings a smaller
> memory footprint and faster attribute access, but the price is flexibility.
>

True, still can be the only way to save few MB or... GB without falling
back to C or PyPy.

Have a look at PyPy to how to save memory (and speed things up) without
slots:
http://morepypy.blogspot.fr/2010/11/efficiently-implementing-python-objects.html

In Python 3 there is a class that is equivalent to:

class foo(object):
    pass

simple object with a __dict__, I can't find it anymore and also there is
SimpleNamespace<https://docs.python.org/3/library/types.html#types.SimpleNamespace>class.

To sum up:

- If you want to create a sentinel value, use object()
- If you want to create an object to hold values and access them as
attributes, use something like SimpleNamespace


> Cheers,
> Cameron Simpson <cs at zip.com.au>
>
> Try being nothing but bored for 4 hours straight, and then tell me that
> there's no fear involved.       - dave at elxr.jpl.nasa.gov (Dave Hayes)
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140423/a6eba199/attachment.html>


More information about the Python-list mailing list