On 8/15/12, Norman Harman <[email protected]> wrote:
> Python idiom is to not write getter / setters until (if) you need
> them. Use attributes instead. Python Descriptors enable this.
> http://docs.python.org/howto/descriptor.html
>
> class Foo():
> def __init__(self):
> self.something = None
>
> users of Foo can get / set .something as they wish. Later on it's
> decided that setting something needs some logic. Foo is updated
>
> def __init__(self):
> self._something = None
>
> def set_something(self, val):
> # do stuff
> self._something = val
> def get_something(self):
> return self._something # or calculate it or whatever
>
> something = property(get_something, set_something)
>
you can also do this in case you don't want get_something and
set_something methods in that class (<= which are not as Pythonic as
uniform attribute access via properties and descriptors in general ;)
{{{
#!python
class Foo():
def __init__(self):
self.something = None
@property
def something(self, val):
# do stuff
self._something = val
@something.setter
def something(self):
return self._something # or calculate it or whatever
}}}
--
Regards,
Olemis.
Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/
Featured article:
--
You received this message because you are subscribed to the Google Groups "Trac
Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/trac-dev?hl=en.