Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24804

Dictless classes

From Steven D'Aprano <steve+comp.lang.python@pearwood.info>
Subject Dictless classes
Newsgroups comp.lang.python
Date 2012-07-03 05:03 +0000
Message-ID <4ff27d28$0$11103$c3e8da3@news.astraweb.com> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Dictless classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-03 05:03 +0000
  Re: Dictless classes alex23 <wuwei23@gmail.com> - 2012-07-02 22:50 -0700
  Re: Dictless classes Duncan Booth <duncan.booth@invalid.invalid> - 2012-07-03 09:38 +0000
    Re: Dictless classes Roy Smith <roy@panix.com> - 2012-07-03 08:28 -0400
  Re: Dictless classes Terry Reedy <tjreedy@udel.edu> - 2012-07-03 19:25 -0400

csiph-web