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


Groups > comp.lang.python > #56720

Metaclass/abc hackery

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.087
X-Spam-Evidence '*H*': 0.83; '*S*': 0.00; '(of': 0.07; 'sure,': 0.09; 'url:github': 0.09; '&quot;i': 0.16; 'dig': 0.16; 'magic': 0.16; 'metaclasses': 0.16; 'metaclasses.': 0.16; 'nonetheless': 0.16; 'throw': 0.16; 'thanks,': 0.17; 'tests': 0.22; 'rid': 0.24; "i've": 0.25; 'subject:/': 0.26; 'idea': 0.28; 'function': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'idea,': 0.31; 'question:': 0.31; 'allows': 0.31; 'class': 0.32; "i'd": 0.34; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'should': 0.36; 'so,': 0.37; 'operating': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'short': 0.38; 'anything': 0.39; 'skip:& 20': 0.39; 'bad': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'exposure': 0.60; 'most': 0.60; 'black': 0.61; 'simple': 0.61; 'due': 0.66; 'bottom': 0.67; 'introduction': 0.68; 'useful.': 0.68; 'acts': 0.74; 'abc': 0.84; 'holes': 0.84; 'absolutely': 0.87
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=L54HPbTbnwRibqAWENKwtdfKL3lR+tTnYy5uwiuek98=; b=p/NAXqtU6d8vVW89L/JKxN1wiF52QDoNCxo+eC/hJ1ujIo13hzuGYSDYiUlhx7DbI1 hL6e99hArFjF6KoKMMFnNGUSu/5gpnH9rA0VGM7kQyix2mIU37PuKutOTTR/wOvudNIr ggpwi9TeiahOwutMFLkiRml04GlVwFEcHAasbgzAwK29qrUZ67fygbPIXVapb6ZCydId IeoNPWGstzP8xuGhEAP4ExUIij7x8aCxhCiAdf8SfOC48ZimUD4k1/CufmFGlAKmxHyP f4p+jM8vWPS0AjJdmSk8wLLZ1LSFNNGLnA5SQ/YxFHTTac1HwdFWjWtmoz4IyaKoOgfh zGZw==
MIME-Version 1.0
X-Received by 10.180.98.105 with SMTP id eh9mr5668184wib.56.1381546063544; Fri, 11 Oct 2013 19:47:43 -0700 (PDT)
Date Fri, 11 Oct 2013 19:47:43 -0700
Subject Metaclass/abc hackery
From Demian Brecht <demianbrecht@gmail.com>
To Python <python-list@python.org>
Content-Type multipart/alternative; boundary=f46d04428cc45bc83104e88242c7
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.1027.1381546064.18130.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1381546065 news.xs4all.nl 15866 [2001:888:2000:d::a6]:38387
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56720

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

As with most I'm sure, short of using abc's, I've had very little exposure
to metaclasses. So, when digging into abc implementation, I figured it
would be a good idea to dig into metaclasses, their usage and actually try
writing one.

What I did may be contrived, but it was fun nonetheless and a good
introduction to how metaclasses can be used (of course, they should only be
used when absolutely required and no other solution is readily available
due to the black magic that happens under the hood): I ended up writing a
proof of concept that's abc-like in nature. However, it doesn't depend on
inheritance. It allows you to say: "I want to make sure that this object
/look/ like this type when instantiated".

Again, simple proof of concept that has holes in it and is likely
altogether a bad idea, but it was fun to throw together nonetheless, so I
thought I'd share: https://gist.github.com/demianbrecht/6944269 (check out
the tests at the bottom for usage).

Working on this though brought up a question: Is there anything in the
data model that acts like "__setattr__" but when operating on a class
definition instead of an instance? I'd be able to get rid of the late_bind
function if something like that's available... Not likely something that
would be used very often, but would likely sometimes be useful.

Thanks,

-- 

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

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


Thread

Metaclass/abc hackery Demian Brecht <demianbrecht@gmail.com> - 2013-10-11 19:47 -0700
  Re: Metaclass/abc hackery Marco Buttu <marco.buttu@gmail.com> - 2013-10-12 06:56 +0200

csiph-web