Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29928 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2012-09-24 14:53 -0600 |
| Last post | 2012-09-24 14:53 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: metaclass question Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-24 14:53 -0600
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-09-24 14:53 -0600 |
| Subject | Re: metaclass question |
| Message-ID | <mailman.1210.1348520018.27098.python-list@python.org> |
On Mon, Sep 24, 2012 at 11:43 AM, Chris Withers <chris@simplistix.co.uk> wrote: > Hi All, > > Is there a metaclass-y way I could cause the following: > > class TheParser(Parser): > def handle_ARecord(self): > pass > def handle_ARecord(self): > pass > > ...to raise an exception as a result of the 'handle_ARecord' name being > reused? In Python 2.x, no. In Python 3.x, the __prepare__ method of the metaclass allows you to specify a custom namespace object for the class definition. The most commonly cited use case is to use an OrderedDict to remember the order in which the attributes are defined, but you could also use a dict subclass that raises an exception if an attribute is redefined. See the docs at: http://docs.python.org/dev/reference/datamodel.html#preparing-the-class-namespace
Back to top | Article view | comp.lang.python
csiph-web