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: 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: References: Date: Thu, 11 Apr 2013 09:19:59 +0100 Subject: Re: name lookup failure using metaclasses with unittests From: Arnaud Delobelle To: Ulrich Eckhardt 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 11 April 2013 07:43, Ulrich Eckhardt 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