Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: metaclass question

Reply
Thread Tools

Re: metaclass question

 
 
Ian Kelly
Guest
Posts: n/a
 
      09-24-2012
On Mon, Sep 24, 2012 at 11:43 AM, Chris Withers <(E-Mail Removed)> 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...lass-namespace
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Metaclass of a metaclass Steven D'Aprano Python 1 06-05-2012 03:30 PM
Question about metaclass Makoto Kuwata Python 1 11-02-2011 04:42 AM
Is a Borg rebellion possible? (a metaclass question) =?iso-8859-1?B?QW5kcuk=?= Python 10 09-08-2007 02:58 AM
metaclass that inherits a class of that metaclass? ironfroggy Python 16 06-03-2005 10:00 AM
A newbie metaclass question could ildg Python 3 05-22-2005 06:10 PM



Advertisments