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: 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; '"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 To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --f46d04428cc45bc83104e88242c7 Content-Type: text/plain; charset=ISO-8859-1 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 --f46d04428cc45bc83104e88242c7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
As with most I'm sure, short of using abc= 9;s, I've had very little exposure to metaclasses. So, when digging int= o abc implementation, I figured it would be a good idea to dig into metacla= sses, their usage and actually try writing one.

What I did may be contrived, but it was fun n= onetheless and a good introduction to how metaclasses can be used (of cours= e, 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 togethe= r nonetheless, so I thought I'd share:=A0https://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 b= e able to get rid of the late_bind function if something like that's av= ailable... Not likely something that would be used very often, but would li= kely sometimes be useful.

Thanks,

--
--f46d04428cc45bc83104e88242c7--