Dictless classes

Discussion in 'Python' started by Steven D'Aprano, Jul 3, 2012.

  1. You can create instances without a __dict__ by setting __slots__:

    py> class Dictless:
    .... __slots__ = ['a', 'b', 'c']
    ....
    py> Dictless().__dict__
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'Dictless' object has no attribute '__dict__'


    But the class itself still has a __dict__:

    py> Dictless.__dict__
    dict_proxy({'a': <member 'a' of 'Dictless' objects>, 'c': <member 'c' of
    'Dictless' objects>, 'b': <member 'b' of 'Dictless' objects>,
    '__module__': '__main__', '__slots__': ['a', 'b', 'c'], '__doc__': None})


    I wonder whether there is some metaclass magic one can do to create a
    class without a __dict__?


    I don't have a use-case for this. But I have some code which assumes that
    every class will have a __dict__, and I wonder whether that is a safe
    assumption.



    --
    Steven
    Steven D'Aprano, Jul 3, 2012
    #1
    1. Advertising

  2. Steven D'Aprano

    alex23 Guest

    On Jul 3, 3:03 pm, Steven D'Aprano <steve
    > wrote:
    > I don't have a use-case for this. But I have some code which assumes that
    > every class will have a __dict__, and I wonder whether that is a safe
    > assumption.


    Remember the recent thread on using a different implementation for
    <object>.__dict__?
    https://groups.google.com/group/python-ideas/browse_frm/thread/f15ef3c7b702a1fa

    Given it was impossible to have it *not* create a conventional dict
    via metaclasses, I would assume that there will always be a .__dict__.
    alex23, Jul 3, 2012
    #2
    1. Advertising

  3. Steven D'Aprano

    Roy Smith Guest

    In article <XnsA0856B054AB3Fduncanbooth@127.0.0.1>,
    Duncan Booth <> wrote:

    > Also you cannot create a subclass of 'type' with non-empty slots (it throws
    > a type error "nonempty __slots__ not supported for subtype of 'type'").
    > However I don't think that's really relevant as even if they did allow you
    > to use __slots__ it wouldn't prevent the creation of a __dict__ for the
    > 'type' base class.


    I played around a bit trying to write a metaclass which implemented:

    def __getattribute__(self, name):
    if name == "__dict__":
    raise AttributeError

    but didn't get very far. Maybe somebody with stronger metaclass-fu than
    I have could run with this idea?
    Roy Smith, Jul 3, 2012
    #3
  4. Steven D'Aprano

    Terry Reedy Guest

    On 7/3/2012 1:03 AM, Steven D'Aprano wrote:
    > You can create instances without a __dict__ by setting __slots__:


    Each *instance* does not have its own dict because each would be equal,
    therefore only one hidden mapping is needed, somewhere. But attribute
    names must be mapped to objects somehow.

    > I wonder whether there is some metaclass magic one can do to create a
    > class without a __dict__?


    If every 'class' instance of the metaclass has the same attributes, then
    it would not be necessary for each 'class' to have its individual dict.
    But would you call such things 'classes'?

    > I don't have a use-case for this. But I have some code which assumes
    > that every class will have a __dict__, and I wonder whether that is a
    > safe assumption.


    It depends what you mean by a class. A 'metaclass' is just a callable
    that accepts 3 args. The only constraint appears that its return must be
    callable (and accept at least one arg).

    class M():
    __slots__ = ()
    def __init__(*args): pass
    def __call__(*args): pass

    class C(metaclass = M): pass

    c=C()
    print(type(C), C, c, hasattr(C, '__dict__'))

    #
    <class '__main__.M'> <__main__.M object at 0x000000000220A1C0> None False

    Is C a dictless class? Your choice ;-).

    Actually, Python thinks not. If we add
    class Mclassob(object): pass
    and have __call__ return an instance thereof and try
    c.__class__ = C
    we get
    TypeError: __class__ must be set to a class, not 'M' object

    --
    Terry Jan Reedy
    Terry Reedy, Jul 4, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,925
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,193
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,392
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    894
    Andrew Thompson
    Jul 17, 2004
  5. Razvan
    Replies:
    5
    Views:
    11,274
    Dale King
    Jul 27, 2004
Loading...

Share This Page