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


Groups > comp.lang.python > #62486

Re: @property simultaneous setter and getter

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jeanpierreda@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; '21,': 0.07; 'nested': 0.07; 'none:': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'decorator': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'a()': 0.16; 'a(object):': 0.16; 'attr': 0.16; 'descriptor': 0.16; 'funcs': 0.16; 'to:addr:web.de': 0.16; 'sat,': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'print': 0.22; 'convenient': 0.24; 'instance,': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'correctly.': 0.31; 'grouping': 0.31; 'class': 0.32; 'probably': 0.32; 'interface': 0.32; 'skip:_ 10': 0.34; "i'd": 0.34; 'classes': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'implement': 0.38; 'skip:p 20': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'making': 0.63; 'fact,': 0.69; 'functions:': 0.84; 'otten': 0.84; 'protocol,': 0.84; 'widespread': 0.91; '2013': 0.98
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 :cc:content-type; bh=ITJ0g+gx4hAxOyQBKqTkfLxcey6I9quNQT7Lg2YO8KQ=; b=FTkrIOB5QRErhMojZytVFELHZYkBBRlZnqCfozuoCDFt0mST6/3iMy7cdbwt68YAQl sUGSmxH5KiLJXZ7Nt9u2GM9pJwCNJA+vevsIa6FjVrT86R3fDBaalBq83zrZTYBF9FaL zi2NwxluXQyLhYZj5yE2EOdptyuNw27Ipe3kZWBOvSf717BCXTr3l25vzXBZI31Tw3pT X/U/q16UaEippcod6E4D7bXGVGIoYJ8fXBP6l+X+cXwwxkrdHGBYYWwMPKYFI2VPj3iL a++3fWw2UY+SyY8p8fjwNddoq1G7mf+94G9qhVCUo+fga32Em19RbwWFtKW2stFQHXTs uYyA==
X-Received by 10.224.47.137 with SMTP id n9mr22718736qaf.47.1387627695447; Sat, 21 Dec 2013 04:08:15 -0800 (PST)
MIME-Version 1.0
In-Reply-To <l93pm0$u3u$1@ger.gmane.org>
References <C1F5DCEF-67D1-410B-9900-C2866AD7882C@gmail.com> <l93pm0$u3u$1@ger.gmane.org>
From Devin Jeanpierre <jeanpierreda@gmail.com>
Date Sat, 21 Dec 2013 04:07:35 -0800
Subject Re: @property simultaneous setter and getter
To Peter Otten <__peter__@web.de>
Content-Type text/plain; charset=UTF-8
Cc "comp.lang.python" <python-list@python.org>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4466.1387627704.18130.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387627704 news.xs4all.nl 2861 [2001:888:2000:d::a6]:54166
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:62486

Show key headers only | View raw


On Sat, Dec 21, 2013 at 2:14 AM, Peter Otten <__peter__@web.de> wrote:
> If I were to implement something like this I'd probably use the old trick
> with nested functions:
>
> def getset(f):
>     funcs = f()
>     return property(funcs.get("get"), funcs.get("set"))
>
> class A(object):
>     @getset
>     def attr():
>         def get(self):
>             print("accessing attr")
>             return self._attr
>         def set(self, value):
>             print("setting attr to {}".format(value))
>             self._attr = value
>         return locals()
>
> if __name__ == "__main__":
>     a = A()
>     a.attr = 42
>     print(a.attr)
>
> This has been around awhile, but I don't see widespread adoption...

IMO: Because classes are the normal way of grouping functions together
in Python.

In fact, we already have an interface for these classes: the
descriptor protocol. Unfortunately it's pretty annoying to define
"properties" directly using the descriptor protocol, because its
__get__ is weird and difficult to remember how to make work correctly.
Also because the decorator making this convenient has been vanished in
python 3. ;)

class A(object):
    _attr = None

    @apply
    class attr(object):
        def __get__(self, instance, owner):
            if instance is None:
                return self

            print "accessing attr"
            return instance._attr

        def __set__(self, instance, value):
            print "setting attr to {}".format(value)
            instance._attr = value

a = A()
a.attr
a.attr = 42


A().attr
A().attr = 3

-- Devin

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


Thread

Re: @property simultaneous setter and getter Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-12-21 04:07 -0800

csiph-web