Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29928

Re: metaclass question

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'attribute': 0.05; 'attributes': 0.07; 'raises': 0.07; 'subject:question': 0.08; 'python': 0.09; 'commonly': 0.09; 'defined,': 0.09; 'dict': 0.09; 'namespace': 0.09; 'sep': 0.09; 'subclass': 0.09; 'def': 0.10; '24,': 0.16; 'definition.': 0.16; 'metaclass': 0.16; 'ordereddict': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'url:dev': 0.17; 'all,': 0.21; 'raise': 0.24; 'allows': 0.25; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'class': 0.29; "skip:' 10": 0.30; 'at:': 0.31; 'url:python': 0.32; 'could': 0.32; 'docs': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'method': 0.36; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'most': 0.61; 'to:name:python': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=D9sOhM28mCmOJcCSM5LqXyAi+2TV93SYkClkxOeYL8w=; b=LHAwcIvRQOSRvYwnhUOlEyNeuk43sLuSGLcqJwFNgO5KjSVWIWw0Ez0JsjeKQOUWXx Z9YCllMd6/AIO/uT8J+UF7eTH/4Fe879OM2flP3GSEftTYfPcyq/+Rrwzpv7KnlkeR/q dU3HyrSrd5pXNVKm1dmXqdKur/gQ29fT1+8FVwNw9q309AcaSaAjS/H9NHkke4xyzs/9 HgrtDuj0vJboC0HHCFdGGTYrdLoSHdTpigzDaIqsbcO1dVj7BCsTfQgl/QHEczOlaKtN 1hAwHBAG3SOGQxAQQNnz7MiEWVHVSZVBnkRGYoxbwaFZqeE52rrQPBqctHVme6EYU6mc 59qQ==
MIME-Version 1.0
In-Reply-To <50609BC5.3020004@simplistix.co.uk>
References <50609BC5.3020004@simplistix.co.uk>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 24 Sep 2012 14:53:06 -0600
Subject Re: metaclass question
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1210.1348520018.27098.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348520018 news.xs4all.nl 6874 [2001:888:2000:d::a6]:46406
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29928

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: metaclass question Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-24 14:53 -0600

csiph-web