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


Groups > comp.lang.python > #21439

Re: stackoverflow question

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'attributes': 0.05; 'attribute': 0.07; 'prints': 0.07; 'raises': 0.07; 'received:verizon.net': 0.07; 'stuff,': 0.07; 'terry': 0.07; 'attribute.': 0.09; 'created,': 0.09; 'elegant,': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; '__init__': 0.16; 'earlier,': 0.16; 'metaclass': 0.16; 'reedy': 0.16; 'test()': 0.16; 'text:': 0.16; 'subject:question': 0.16; 'wrote:': 0.18; 'have:': 0.18; 'instance': 0.18; 'rewrite': 0.18; 'subject:skip:s 10': 0.18; 'jan': 0.19; 'seems': 0.20; 'header:In- Reply-To:1': 0.22; 'tests.': 0.23; 'works.': 0.23; 'hey': 0.24; 'tests': 0.25; 'code': 0.26; 'value.': 0.28; "i'm": 0.28; 'posted': 0.29; 'class': 0.29; 'pm,': 0.29; 'confusion': 0.30; 'values': 0.32; 'anyone': 0.32; 'error.': 0.32; "isn't": 0.33; 'there': 0.33; "won't": 0.33; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'object': 0.33; 'header:X-Complaints-To:1': 0.34; 'normally': 0.34; 'certain': 0.34; 'to:addr:python-list': 0.35; 'none': 0.35; 'something': 0.35; 'question': 0.36; 'received:org': 0.36; 'created': 0.37; 'run': 0.37; 'but': 0.37; 'either': 0.37; 'skip:_ 10': 0.38; 'could': 0.38; 'some': 0.38; 'processing': 0.39; 'put': 0.40; 'to:addr:python.org': 0.40; 'here': 0.64; 'idiom': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: stackoverflow question
Date Fri, 09 Mar 2012 18:16:32 -0500
References <4F5A7FCA.6030109@stoneleaf.us>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <4F5A7FCA.6030109@stoneleaf.us>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.537.1331335028.3037.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331335028 news.xs4all.nl 6947 [2001:888:2000:d::a6]:52879
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21439

Show key headers only | View raw


On 3/9/2012 5:10 PM, Ethan Furman wrote:
> Hey all!
>
> I posted a question/answer on SO earlier, but there seems to be some
> confusion around either the question or the answer (judging from the
> comments).
>
> http://stackoverflow.com/q/9638921/208880
>
> If anyone here is willing to take a look at it and let me know if I did
> not write it well, I would appreciate the feedback
>
> Here's the question text:
> ------------------------
> I'm writing a metaclass to do some cool stuff, and part of its
> processing is to check that certain attributes exist when the class is
> created. Some of these are mutable, and would normally be set in
> `__init__`, but since `__init__` isn't run until the instance is created
> the metaclass won't know that the attribute *will* be created, and
> raises an error.
a. Create an instance and see if it has the attribute. Then delete it.
b. Put such tests in unit tests.
c. Check the code object to see if the attribute will be created.

> I could do something like:
>
> class Test(meta=Meta):
> mutable = None
> def __init__(self):
> self.mutable = list()
>
> But that isn't very elegant, and also violates DRY.

It works. It is a standard idiom for default values that are 
conditionally masked with an instance value.
>
> What I need is some way to have:
>
> class Test(metaclass=Meta):
> mutable = list()
>
> t1 = Test()
> t2 = Test()
> t1.mutable.append('one')
> t2.mutable.append('two')
> t1.mutable # prints ['one']
> t2.mutable # prints ['two']
>
> Any ideas on how this can be accomplished?

Rewrite the __init__ code object ;-).

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: stackoverflow question Terry Reedy <tjreedy@udel.edu> - 2012-03-09 18:16 -0500

csiph-web