Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'needed,': 0.05; 'class,': 0.07; 'python': 0.09; 'accepts': 0.09; 'args.': 0.09; 'callable': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'typeerror:': 0.09; 'def': 0.10; '*instance*': 0.16; '__slots__': 0.16; 'attributes,': 0.16; 'mapped': 0.16; 'message-id:@dough.gmane.org': 0.16; 'metaclass': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'somehow.': 0.16; 'somewhere.': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'jan': 0.18; 'appears': 0.18; 'assumes': 0.22; 'class.': 0.23; 'pass': 0.25; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'am,': 0.27; 'wonder': 0.27; 'header:X -Complaints-To:1': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'thinks': 0.29; 'objects': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'this.': 0.29; 'code': 0.31; '(and': 0.32; 'not.': 0.32; 'instances': 0.33; 'to:addr:python-list': 0.33; 'false': 0.35; 'mapping': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'depends': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'object': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'safe': 0.63; 'therefore': 0.65; "'class'": 0.84; 'actually,': 0.84; 'dict.': 0.84; 'received:fios.verizon.net': 0.84; 'thereof': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Dictless classes Date: Tue, 03 Jul 2012 19:25:42 -0400 References: <4ff27d28$0$11103$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: <4ff27d28$0$11103$c3e8da3@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341357962 news.xs4all.nl 6880 [2001:888:2000:d::a6]:54495 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24840 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__')) # <__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