Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62481 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2013-12-21 11:14 +0100 |
| Last post | 2013-12-21 11:14 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: @property simultaneous setter and getter Peter Otten <__peter__@web.de> - 2013-12-21 11:14 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-12-21 11:14 +0100 |
| Subject | Re: @property simultaneous setter and getter |
| Message-ID | <mailman.4465.1387620889.18130.python-list@python.org> |
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...
Back to top | Article view | comp.lang.python
csiph-web