Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; '"""': 0.07; 'nested': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:github': 0.09; 'def': 0.12; '@property': 0.16; 'a()': 0.16; 'a(object):': 0.16; 'attr': 0.16; 'funcs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'value=none):': 0.16; 'wrote:': 0.18; 'example': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'external': 0.29; 'code': 0.31; 'piece': 0.31; 'class': 0.32; 'probably': 0.32; 'brian': 0.33; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'implement': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'such': 0.63; 'functions:': 0.84; 'widespread': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: @property simultaneous setter and getter Date: Sat, 21 Dec 2013 11:14:37 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b740.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387620889 news.xs4all.nl 2945 [2001:888:2000:d::a6]:34230 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62481 Brian Bruggeman wrote: > Is this something that would be pep-able? > > https://gist.github.com/brianbruggeman/8061774 There's no need to put such a small piece of code into an external repository. > class someAwesomeClass(object): > """ an example """ > > @property > def some_attr(self, value=None): > """ > Implementation of a setter and getter property in one > """ > attr = "__local_attr__" > if not hasattr(self, attr): > setattr(self, attr, value) > if (value != None and hasattr(self, attr)): > setattr(self, attr, value) > return getattr(self, attr) 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...