Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder7.xlned.com!news2.euro.net!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; 'properly.': 0.07; 'try:': 0.07; 'subject:help': 0.07; '###': 0.09; '**kwargs):': 0.09; 'behave': 0.09; 'dict': 0.09; 'idea?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'aug': 0.13; 'properly': 0.15; '10x': 0.16; 'clear(self):': 0.16; 'items(self):': 0.16; 'keys(self):': 0.16; 'losing': 0.16; 'massimo': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'self[key]': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'skip:u 30': 0.17; '>>>': 0.18; 'trying': 0.21; 'skip:_ 20': 0.22; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'charset:iso-8859-15': 0.26; '(3)': 0.27; 'header:X-Complaints- To:1': 0.28; 'lines': 0.28; 'dictionary': 0.29; 'key,': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; '(2)': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; '(1)': 0.34; 'self': 0.34; 'faster': 0.35; 'add': 0.36; 'received:org': 0.36; 'really': 0.36; 'except': 0.36; 'but': 0.36; "didn't": 0.36; 'method': 0.36; 'skip:p 20': 0.36; 'ok,': 0.37; 'does': 0.37; 'why': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'cast': 0.65; '(5)': 0.71; 'casting': 0.84; 'oscar': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Oscar Benjamin Subject: Re: Class.__class__ magic trick help Date: Tue, 21 Aug 2012 08:40:40 +0100 References: <21067364-7b9c-4415-bad1-947961497343@u20g2000yqc.googlegroups.com> <8dc49931-de0d-4ce3-8b0d-8789bc8c07c8@w9g2000yqe.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: cpc1-aztw8-0-0-cust1455.18-1.cable.virginmedia.com In-Reply-To: <8dc49931-de0d-4ce3-8b0d-8789bc8c07c8@w9g2000yqe.googlegroups.com> User-Agent: Groundhog Newsreader for Android 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: 129 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345534858 news.xs4all.nl 6864 [2001:888:2000:d::a6]:52882 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27555 On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro wrote: > Consider this code: > class SlowStorage(dict): > def __getattr__(self,key): > return self[key] > def __setattr__(self,key): > self[key]=value > class FastStorage(dict): > def __init__(self, __d__=None, **kwargs): > self.update(__d__,**kwargs) > def __getitem__(self,key): > return self.__dict__.get(key,None) > def __setitem__(self,key,value): > self.__dict__[key] = value > def __delitem__(self,key): > delattr(self,key) > def __copy__(self): > return Storage(self) > def __nonzero__(self): > return len(self.__dict__)>0 > def pop(self,key,default=None): > if key in self: > default = getattr(self,key) > delattr(self,key) > return default > def clear(self): > self.__dict__.clear() > def __repr__(self): > return repr(self.__dict__) > def keys(self): > return self.__dict__.keys() > def values(self): > return self.__dict__.values() > def items(self): > return self.__dict__.items() > def iterkeys(self): > return self.__dict__.iterkeys() > def itervalues(self): > return self.__dict__.itervalues() > def iteritems(self): > return self.__dict__.iteritems() > def viewkeys(self): > return self.__dict__.viewkeys() > def viewvalues(self): > return self.__dict__.viewvalues() > def viewitems(self): > return self.__dict__.viewitems() > def fromkeys(self,S,v=None): > return self.__dict__.fromkeys(S,v) > def setdefault(self, key, default=None): > try: > return getattr(self,key) > except AttributeError: > setattr(self,key,default) > return default > def clear(self): > self.__dict__.clear() > def len(self): > return len(self.__dict__) > def __iter__(self): > return self.__dict__.__iter__() > def has_key(self,key): > return key in self.__dict__ > def __contains__(self,key): > return key in self.__dict__ > def update(self,__d__=None,**kwargs): > if __d__: > for key in __d__: > kwargs[key] = __d__[key] > self.__dict__.update(**kwargs) > def get(self,key,default=None): > return getattr(self,key) if key in self else default > >>> s=SlowStorage() > >>> a.x=1 ### (1) > >>> a.x ### (2) > 1 # ok > >>> isinstance(a,dict) > True # ok > >>> print dict(a) > {'x':1} # ok (3) Try: >>> a.items() What does that show? > >>> s=FastStorage() > >>> a.x=1 ### (4) > >>> a.x ### (5) > 1 # ok > >>> isinstance(a,dict) > True # ok > >>> print dict(a) > {} # not ok (6) > Lines (4) and (5) are about 10x faster then lines (1) and (2). I like > FastStorage better but while (3) behaves ok, (6) does not behave as I > want. > I intuitively understand why FastStorage is cannot cast into dict > properly. > What I do not know is how to make it do the casting properly without > losing the 10x speedup of FastStorage over SlowStorage. > Any idea? I don't really understand what your trying to do but since you didn't add the __setattr__ method to FastStorage the item is not added to the dictionary when you do a.x = 1 Oscar