Accessors in Python (getters and setters)

Bruno Desthuilliers onurb at xiludom.gro
Thu Jul 13 04:49:42 EDT 2006


mystilleef wrote:
(snip)
> Python doesn't have any philosophy with regards to naming identifiers.

Yes it does.

> 
>>But they are in Python and that is the python's philosophy. All attribute or
>>method not beginning with an '_' *is* API.
> 
> Right, and what if I want to change a private API to a public one. 

Then you provide a public API on top of the private one.

class MyClass(object):
  def __init__(self, ...):
     self._attr = XXX

  # seems like we really have enough use
  # cases to justify exposing _imp_attr
  @apply
  def attr():
    def fget(self):
      return self._attr
    def fset(self):
      self._attr = attr
    return property(**locals())


  def _method(self, ...):
    # code here

  # seems like we really have enough use
  # cases to justify exposing _imp_method
  method = _impmethod

Note that none of this actually breaks encapsulation.

> How
> does that solve my naming issues.

How could this solve *your* naming issue ? This is totally unrelated.
You choose a bad name for a *public* symbol.

>>And in python the reverse can be true :
> 
> The reverse is hardly ever true. 

So what are computed attributes ?

> 90% of public APIs in almost all
> languages are methods or functions.

"allmost all languages" lacks computed attributes.

-- 
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