Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43337
| References | <sqjf3a-03s.ln1@satorlaser.homedns.org> <mailman.404.1365587492.3114.python-list@python.org> <em2i3a-hj2.ln1@satorlaser.homedns.org> |
|---|---|
| Date | 2013-04-11 09:19 +0100 |
| Subject | Re: name lookup failure using metaclasses with unittests |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.448.1365668407.3114.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Find similar | Unroll 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