How do these Java concepts translate to Python?

Ray ray_usenet at yahoo.com
Fri Aug 12 10:09:13 EDT 2005



bruno modulix wrote:
<snipped>
> And BTW, don't bother making all your attributes "protected" or
> "private" then writing getters and setters, Python has a good support
> for computed attributes, so you can change the implementation without
> problem (which is the original reason for not-public attributes):

Thanks Bruno! This is good stuff. This is exactly what I want to avoid:
writing Java in Python.

Cheers,
Ray

>
> # first version:
> class Foo(object):
>   def __init__(self):
>      self.baaz = 42 # public attribute
>
> # later we discover that we want Foo.baaz to be computed:
> class Foo(object):
>   def __init__(self):
>      self.baaz = 42
>
>   def _set_baaz(self, value):
>      if value < 21 or value > 84:
>         raise ValueError, "baaz value must be in range 21..84"
>      self._baaz = value
>
>   def _get_baaz(self):
>       return self._baaz * 2
>
>   baaz = property(fget=_get_baaz, fset=_set_baaz)
>
> Easy as pie, uh ?-)
>
>
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in 'onurb at xiludom.gro'.split('@')])"




More information about the Python-list mailing list