Re: metaclass question

Discussion in 'Python' started by Ian Kelly, Sep 24, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Mon, Sep 24, 2012 at 11:43 AM, Chris Withers <> 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
     
    Ian Kelly, Sep 24, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. could ildg

    A newbie metaclass question

    could ildg, May 22, 2005, in forum: Python
    Replies:
    3
    Views:
    314
    Steven Bethard
    May 22, 2005
  2. ironfroggy
    Replies:
    16
    Views:
    443
    Michele Simionato
    Jun 3, 2005
  3. =?iso-8859-1?B?QW5kcuk=?=

    Is a Borg rebellion possible? (a metaclass question)

    =?iso-8859-1?B?QW5kcuk=?=, Sep 7, 2007, in forum: Python
    Replies:
    10
    Views:
    536
    BartlebyScrivener
    Sep 8, 2007
  4. Makoto Kuwata

    Question about metaclass

    Makoto Kuwata, Nov 2, 2011, in forum: Python
    Replies:
    1
    Views:
    152
    Patrick Maupin
    Nov 2, 2011
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    314
Loading...

Share This Page