Path: csiph.com!usenet.pasdenom.info!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'attributes': 0.09; 'url:github': 0.09; 'def': 0.12; '__new__': 0.16; 'bases,': 0.16; 'dict': 0.16; 'expecting': 0.16; 'foo(object):': 0.16; 'inheritance': 0.16; 'magic': 0.16; 'subclass': 0.16; 'appropriate': 0.16; 'thanks,': 0.17; '(not': 0.18; 'hey': 0.18; 'all,': 0.19; 'trying': 0.19; 'implementing': 0.19; 'passing': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'case.': 0.24; 'rid': 0.24; "i've": 0.25; 'class.': 0.26; 'purposes': 0.26; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'apparently': 0.31; 'enforce': 0.31; 'class': 0.32; 'figure': 0.32; 'interface': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'curious': 0.36; 'doing': 0.36; 'method': 0.36; 'easily': 0.37; 'being': 0.38; 'expected': 0.38; 'implement': 0.38; 'skip:o 20': 0.38; 'checks': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; "you're": 0.61; '/only/': 0.84; "class's": 0.84; 'subject:Using': 0.84; 'favour': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=PHyCiB7hW022YRg+FA277JibEcp7biW03Ma2RFb2pew=; b=JxMfEB90rvaS6jKU+HlNXUAHCKdL5PfWTUaPoQ7cKE8bNolWCoXm28Z+Jcf1eBeSUZ 4hNcC4CvHrzIb5eS6lNdxr37fsPeC1koRp/Yy/bWefOk1JeH+qHP0x9kgZnySimQDFpn oMyFFUvJXm6PU1dRp4za9qDxBHxO597XMsvsxNXYek0nM2nGdl6HTKkfWYTvWCw+GTVR ezs470aPUgG6OiknmuyJvMISANdDwVYwBIN7ebBtKO2jRzgT/qJHj1jY6k+pNF39HoSO r+HXfylJYdRgFIHYoyeqOsD0Unc5QhYqDnWHhcCmkUZpPj97UVF1W5R28cs+QlAViQif OxDw== MIME-Version: 1.0 X-Received: by 10.180.187.237 with SMTP id fv13mr3342478wic.26.1392308102045; Thu, 13 Feb 2014 08:15:02 -0800 (PST) Date: Thu, 13 Feb 2014 08:15:02 -0800 Subject: Using a subclass for __dict__ From: Demian Brecht To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392308103 news.xs4all.nl 2875 [2001:888:2000:d::a6]:55623 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66188 Hey all, Using bases in a metaclass, I've been able to easily figure out when an attribute is being added to an instance of a class. However, what I'm /actually/ looking to do is to intercept when attributes are being added to a class (not an instance of). I thought that I'd be able to do so by passing in a subclass of dict to type.__new__ of the metaclass's __new__ and implementing a custom __setitem__, but apparently that's not the case. If you're curious as to why I'm doing this, I'm trying to clean up https://gist.github.com/demianbrecht/6944269 and get rid of the late_bind method there in favour of using the appropriate magic methods. The intention is to piggyback off of abc's __abstractmethods__ in order to implement a "looks_like" method that checks that one class conforms to an unrelated class's interface rather than using inheritance to enforce interface implementations at class creation time. This is /only/ intended to be a proof of concept for demonstration purposes and nothing that I'd ever actually implement, so no need for flaming the concept :) # test.py class Dict(dict): def __setattr__(self, key, value): print 'Dict.__setattr__' dict.__setattr__(self, key, value) def __setitem__(self, key, value): print 'Dict.__setitem__' dict.__setitem__(self, key, value) class Bar(object): def __setattr__(self, key, value): print 'Bar.__setattr__' object.__setattr__(self, key, value) class metafoo(type): def __new__(mcls, name, bases, dct): return type.__new__(mcls, name, (Bar,), Dict(dct)) class Foo(object): __metaclass__ = metafoo >>> import test >>> f = test.Foo() >>> f.foo = 'bar' Bar.__setattr__ # expected >>> test.Foo.foo = 'bar' # I'm expecting Dict.__setattr__ here, but... *crickets* Am I missing something here, or do I just have to live with what I currently have in my gist? Thanks, -- Demian Brecht http://demianbrecht.github.com