Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21472
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ethan@stoneleaf.us> |
| 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; 'new,': 0.04; 'attributes': 0.05; 'passes': 0.05; '"""': 0.07; 'attribute': 0.07; 'none:': 0.07; 'prints': 0.07; 'raises': 0.07; 'skip:` 10': 0.07; 'stuff,': 0.07; 'will,': 0.07; '>>>>': 0.09; 'created,': 0.09; 'elegant,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'mutable': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.13; 'feedback.': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; '*args,': 0.16; '__init__': 0.16; 'devil': 0.16; 'earlier,': 0.16; 'functools': 0.16; 'metaclass': 0.16; 'received:50': 0.16; 'replaces': 0.16; 'test()': 0.16; 'text:': 0.16; 'with?': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'have:': 0.18; 'instance': 0.18; 'subject:skip:s 10': 0.18; 'later': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'skip:m 30': 0.24; 'hey': 0.24; 'otherwise,': 0.25; 'cc:2**0': 0.26; 'classes': 0.26; 'function': 0.27; 'there.': 0.27; 'import': 0.27; 'value.': 0.28; "i'm": 0.28; 'posted': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.29; 'confusion': 0.30; 'good.': 0.32; 'anyone': 0.32; 'error.': 0.32; 'idea': 0.32; 'list': 0.32; "isn't": 0.33; 'there': 0.33; "won't": 0.33; 'header :User-Agent:1': 0.33; 'named': 0.33; 'object': 0.33; 'checking': 0.34; 'normally': 0.34; 'certain': 0.34; 'set.': 0.34; 'none': 0.35; 'something': 0.35; 'list.': 0.35; 'question': 0.36; 'created': 0.37; 'run': 0.37; 'passed': 0.37; 'but': 0.37; 'either': 0.37; 'skip:_ 10': 0.38; 'replace': 0.38; 'could': 0.38; 'some': 0.38; 'doing': 0.38; 'processing': 0.39; 'unless': 0.39; 'type': 0.61; 'your': 0.61; 'here': 0.64; 'received:websitewelcome.com': 0.64; 'said:': 0.67; 'care': 0.71; '[3]': 0.73; 'details,': 0.74; 'owen': 0.84; 'received:gateway14.websitewelcome.com': 0.84 |
| Date | Sat, 10 Mar 2012 14:21:55 -0800 |
| From | Ethan Furman <ethan@stoneleaf.us> |
| User-Agent | Thunderbird 2.0.0.24 (Windows/20100228) |
| MIME-Version | 1.0 |
| To | Owen Jacobson <angrybaldguy@gmail.com> |
| Subject | Re: stackoverflow question |
| References | <mailman.536.1331332491.3037.python-list@python.org> <2012031015032562036-angrybaldguy@gmailcom> |
| In-Reply-To | <2012031015032562036-angrybaldguy@gmailcom> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - gator410.hostgator.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - stoneleaf.us |
| X-BWhitelist | no |
| X-Source | |
| X-Source-Args | |
| X-Source-Dir | |
| X-Source-Sender | c-50-137-149-57.hsd1.or.comcast.net ([192.168.74.2]) [50.137.149.57]:1621 |
| X-Source-Auth | ethan+stoneleaf.us |
| X-Email-Count | 1 |
| X-Source-Cap | dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== |
| Cc | python-list@python.org |
| 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.553.1331419892.3037.python-list@python.org> (permalink) |
| Lines | 106 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1331419892 news.xs4all.nl 6884 [2001:888:2000:d::a6]:55073 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:21472 |
Show key headers only | View raw
Owen Jacobson wrote:
> On 2012-03-09 22:10:18 +0000, Ethan Furman said:
>
>> 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. 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.
>>
>> 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?
>>
>> Also, the metaclass doing the checking doesn't care what type of
>> object the attribute is, only that it is there.
>> ---------------------------
>
> Why check what you can ensure? The __init__ function your metaclass
> passes to type() doesn't have to be the __init__ method your metaclass
> received. Consider the following:
>
>>>> import functools as f
>>>>
>>>> def make_init(real_init):
>>>> """Define an __init__ method that ensures ``self.mutable`` is
>>>> set. If the
>>>> passed ``real_init`` function later replaces ``self.mutable``,
>>>> that value
>>>> is preserved; otherwise, ``self.mutable`` is set to a new, empty
>>>> list.
>>>>
>>>> Arguments to the generated ``__init__`` method are passed to the
>>>> original
>>>> ``real_init`` unchanged.
>>>> """
>>>> def __init__(self, *args, **kwargs):
>>>> self.mutable = list()
>>>> if real_init is not None:
>>>> return real_init(self, *args, **kwargs)
>>>> if real_init is not None:
>>>> f.update_wrapper(__init__, real_init)
>>>> return __init__
>>>>
>>>> class Meta(type):
>>>> def __new__(meta, name, parents, attributes):
>>>> attributes['__init__'] =
>>>> make_init(attributes.get('__init__', None))
>>>> return type.__new__(Meta, name, parents, attributes)
>>>>
>>>> class C(object):
>>>> __metaclass__ = Meta
>>>>
>>>> a, b = C(), C()
>>>>
>>>> a.mutable.append(3)
>>>> b.mutable.append(5)
>>>>
>>>> a.mutable
> [3]
>>>> b.mutable
> [5]
>
> All instances of classes whose metaclass is Meta will, guaranteed, have
> an instance field named 'mutable'. Its value is a list created at
> instance creation time, unless the instance's __init__ provides a
> different value.
The idea is good. The devil is in the details, as usual. How is the
metaclass going to know:
1) which attributes to replace
2) what to replace them with?
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
stackoverflow question Ethan Furman <ethan@stoneleaf.us> - 2012-03-09 14:10 -0800
Re: stackoverflow question Owen Jacobson <angrybaldguy@gmail.com> - 2012-03-10 15:03 -0500
Re: stackoverflow question Ethan Furman <ethan@stoneleaf.us> - 2012-03-10 14:21 -0800
Re: stackoverflow question Owen Jacobson <angrybaldguy@gmail.com> - 2012-03-11 00:48 -0500
csiph-web