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


Groups > comp.lang.python > #66227

Re: Using a subclass for __dict__

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <demianbrecht@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.027
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'interpreter': 0.05; 'implemented.': 0.09; 'implements': 0.09; 'try:': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '3.3,': 0.16; 'a(object):': 0.16; 'b()': 0.16; 'class).': 0.16; 'frozenset': 0.16; 'lambda:': 0.16; 'metaclass': 0.16; 'to:addr:web.de': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'thu,': 0.19; 'seems': 0.21; 'feb': 0.22; 'import': 0.22; '(in': 0.22; 'cc:addr:python.org': 0.22; 'keyboard': 0.24; 'url:dev': 0.24; 'fine': 0.24; 'initial': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'am,': 0.29; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; '13,': 0.31; 'cool,': 0.31; 'purely': 0.31; 'class': 0.32; 'thanks!': 0.32; 'interface': 0.32; 'url:python': 0.33; 'becomes': 0.33; 'implemented': 0.33; 'used,': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'words,': 0.36; 'method': 0.36; 'url:org': 0.36; 'too': 0.37; 'level': 0.37; 'being': 0.38; 'checks': 0.38; 'question,': 0.38; 'issue': 0.38; 'that,': 0.38; 'ensure': 0.60; 'even': 0.60; 'easy': 0.60; 'back': 0.62; "you've": 0.63; 'high': 0.63; 'more': 0.64; 'present.': 0.74; 'goal': 0.75; 'glad': 0.83; "'foo'": 0.84; '2.7.': 0.84; 'abc': 0.84; 'compiles': 0.84; 'otten': 0.84; 'subject:Using': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; 'do:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=sH1LfMig7PEk4YuK1EnsX7q4VVIhnL0yf476LVrpIkw=; b=KJlxX6iGvHsKGdceP61f0hwKbep4Eq5mkHQEtb0jS6totlv3yARPoin5jx0e5ep/eZ my1XtQGEFHtwlCq8PLgdbUbXLNA4oYD+Nl3n4oYsExNUBUXEXEVzXw6EgCpVR6lHP0rS 360Vca1sPvtrMFUnAj0u7NDQM5RsCp5F0YF9e0fOwctWKUFSCAlMhgMApXrh/RdDCtXA HRM9jtcul+4j1EIDCRFD6ffzB6/1X9iPuhaN0s+pszVHbAoZW7z6OhyaJfkbWEz1mfGJ NGYxfZm4ExS/phv/fHtbjj1lPKums/Yk0tUfjT/zz/6AGxbXBqxbj4QusB316BR1nvZg XHYw==
MIME-Version 1.0
X-Received by 10.194.192.233 with SMTP id hj9mr14814wjc.78.1392322029939; Thu, 13 Feb 2014 12:07:09 -0800 (PST)
In-Reply-To <ldj5ai$iuj$1@ger.gmane.org>
References <CAE+T62bYFSdYmYRbY1Cuz51zym-NMYv0O5CnJvsbTR41oUZsag@mail.gmail.com> <ldj5ai$iuj$1@ger.gmane.org>
Date Thu, 13 Feb 2014 12:07:09 -0800
Subject Re: Using a subclass for __dict__
From Demian Brecht <demianbrecht@gmail.com>
To Peter Otten <__peter__@web.de>
Content-Type text/plain; charset=ISO-8859-1
Cc Python <python-list@python.org>
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.6871.1392322032.18130.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392322032 news.xs4all.nl 2907 [2001:888:2000:d::a6]:48934
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66227

Show key headers only | View raw


On Thu, Feb 13, 2014 at 11:10 AM, Peter Otten <__peter__@web.de> wrote:
> 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>

Even though you've already answered my question, here's what I'm trying to do:

High level goal (again, purely proof of concept): Piggy back on
CPython's abc implementation in order to ensure that class A
implements the interface implemented by unrelated class B.

Using abc's as intended, when @abc.abstractmethod is used, it tags the
method with __isabstractmethod__. When ABCMeta.__new__ executes, it
compiles a frozenset (__abstractmethods__) of all methods still tagged
as __isabstractmethod__ (in other words, those that haven't been
implemented in a child class). During object creation in the
interpreter layer, it checks to see if there are any
__abstractmethods__. If there is, then it fails at that point, citing
those methods as not being implemented.

All I did (and it definitely needs some good cleaning) is inject
abstractmethods in class A where implementations matching class B's
interface were not present. The specific problem that I ran into was
when methods are bound to a class definition after initial class
construction. Having said that, this seems to be more of an issue with
2.7's abc implemention:

import abc

class A(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def fn(self): pass


class B(A):
    pass

try:
    B()
except TypeError:
    pass

B.fn = lambda: 'foo'

B()

The above code works fine in 3.3, but fails in 2.7. Cool, glad that's explained.

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

*Throws keyboard across the office*

FFS. I could have SWORN I tried that (because I /know/ that a class is
an instance of its metaclass :/). An instance of looking at something
far too long without a fresh set of eyes.

Thanks!

-- 
Demian Brecht
http://demianbrecht.github.com

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


Thread

Re: Using a subclass for __dict__ Demian Brecht <demianbrecht@gmail.com> - 2014-02-13 12:07 -0800

csiph-web