Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

metaclass question

11 views
Skip to first unread message

Chris Withers

unread,
Sep 24, 2012, 1:43:33 PM9/24/12
to Python List
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?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Ian Kelly

unread,
Sep 24, 2012, 4:53:06 PM9/24/12
to Python
On Mon, Sep 24, 2012 at 11:43 AM, Chris Withers <ch...@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
0 new messages