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


Groups > comp.lang.python > #66206

Re: Using a subclass for __dict__

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'attribute': 0.07; 'attributes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:github': 0.09; 'python': 0.11; '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; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subclass': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; '(not': 0.18; 'bit': 0.19; 'trying': 0.19; 'implementing': 0.19; 'passing': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'rid': 0.24; 'url:dev': 0.24; "i've": 0.25; 'class.': 0.26; 'purposes': 0.26; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; "skip:' 10": 0.31; '>>>>': 0.31; 'apparently': 0.31; 'enforce': 0.31; 'class': 0.32; 'figure': 0.32; 'interface': 0.32; 'url:python': 0.33; 'becomes': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'curious': 0.36; 'doing': 0.36; 'method': 0.36; 'url:org': 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; 'received:org': 0.40; 'easy': 0.60; "you're": 0.61; 'offer': 0.62; 'name': 0.63; 'more': 0.64; '/only/': 0.84; "class's": 0.84; 'subject:Using': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; 'favour': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Using a subclass for __dict__
Date Thu, 13 Feb 2014 20:10:30 +0100
Organization None
References <CAE+T62bYFSdYmYRbY1Cuz51zym-NMYv0O5CnJvsbTR41oUZsag@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdb989.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6855.1392318635.18130.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392318635 news.xs4all.nl 2832 [2001:888:2000:d::a6]:39645
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66206

Show key headers only | View raw


Demian Brecht wrote:

> 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?

I don't really understand what you are trying to do in the gist, perhaps you want

<http://docs.python.org/dev/reference/datamodel.html#customizing-instance-and-subclass-checks>

?

Anyway, intercepting __setattr__() in the class becomes easy once you remember that a class is just 
an instance of its metaclass:

>>> class FooMeta(type):
...     def __setattr__(self, name, value):
...             print value, "-->", name
...             super(FooMeta, self).__setattr__(name, value)
... 
>>> class Foo:
...     __metaclass__ = FooMeta
... 
>>> Foo.bar = "baz"
baz --> bar

Python 3 has a bit more to offer in this area, see

<http://docs.python.org/dev/reference/datamodel.html#preparing-the-class-namespace>

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


Thread

Re: Using a subclass for __dict__ Peter Otten <__peter__@web.de> - 2014-02-13 20:10 +0100

csiph-web