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


Groups > comp.lang.python > #43337

Re: name lookup failure using metaclasses with unittests

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
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; 'else:': 0.03; 'subject:using': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "wouldn't": 0.14; 'bases,': 0.16; 'eckhardt': 0.16; 'metaclass': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.26; 'pass': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'received:209.85.217': 0.29; 'message-id:@mail.gmail.com': 0.30; 'keyerror:': 0.31; 'though.': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'received:209.85': 0.35; 'except': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'provide': 0.64; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=IStyNuOez9NdF+0s+XS9uSkobpPFvsZoGwkGcj87FGg=; b=iEqsoiSeapDGUwPwyyg01hUbO+WEeE/znuqw/vOlHGCVhhSWBDgazYu3VpPI8jA5BM ITOkSvEHz/GkpIaH6IPleUSrpIxeTqLKY1Zudhmg87UlqNQfmovN1yzpYBZ/GsOAdZcC hIFXrH6XBIQiX/ZghzV2wtrnOs5uZ9qXV4c7nxsYcMXiDSjMJ+72DIPDr3hlIfVXxJZo fFPu/B1s9J4iY95m5QQy8TRippPLfE63JJTa+fiEUMk4oyR029CdziuIjFyV8gLCv5Op 8r29rbqFBwqzBpr2xdwz5+MxdRlnEiP12zNe891CBX8oBp5OcVecxGRZhVs/MLBUPXDN +TlA==
MIME-Version 1.0
X-Received by 10.152.4.131 with SMTP id k3mr269002lak.26.1365668399195; Thu, 11 Apr 2013 01:19:59 -0700 (PDT)
In-Reply-To <em2i3a-hj2.ln1@satorlaser.homedns.org>
References <sqjf3a-03s.ln1@satorlaser.homedns.org> <mailman.404.1365587492.3114.python-list@python.org> <em2i3a-hj2.ln1@satorlaser.homedns.org>
Date Thu, 11 Apr 2013 09:19:59 +0100
Subject Re: name lookup failure using metaclasses with unittests
From Arnaud Delobelle <arnodel@gmail.com>
To Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Content-Type text/plain; charset=UTF-8
Cc 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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.448.1365668407.3114.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365668407 news.xs4all.nl 2683 [2001:888:2000:d::a6]:57769
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43337

Show key headers only | View raw


On 11 April 2013 07:43, Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> wrote:

> The second question that came up was if there is a way to keep a metaclass
> defined inside the class or if the only way is to provide it externally.

Yes, using metaclasses!  I wouldn't recommend it though.  Here's a
proof of concept:

class MyType(type):
    def __new__(meta, name, bases, attrs):
        try:
            metaclass = attrs.pop('__metaclass__')
        except KeyError:
            return type.__new__(meta, name, bases, attrs)
        else:
            return metaclass(name, bases, attrs)

class MyObject(metaclass=MyType):
    pass


>>> class Test(MyObject):
...     def __metaclass__(name, bases, attrs):
...         print("Test metaclass")
...         return MyType(name, bases, attrs)
...
Test metaclass

-- 
Arnaud

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


Thread

name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-10 10:18 +0200
  Re: name lookup failure using metaclasses with unittests Peter Otten <__peter__@web.de> - 2013-04-10 11:52 +0200
    Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-11 09:09 +0200
    Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-11 08:43 +0200
      Re: name lookup failure using metaclasses with unittests Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-11 08:19 +0000
        Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-12 09:17 +0200
          Re: name lookup failure using metaclasses with unittests Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-12 10:58 -0400
      Re: name lookup failure using metaclasses with unittests Arnaud Delobelle <arnodel@gmail.com> - 2013-04-11 09:19 +0100

csiph-web