is python Object oriented??

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 3 15:57:29 EST 2009


En Mon, 02 Feb 2009 19:51:11 -0200, Russ P. <Russ.Paielli at gmail.com>  
escribió:

> Suppose a library developer (or a module developer on a large team)
> uses leading underscores. Now suppose that, for whatever reason
> (pressure from the users, perhaps), the library developer decides to
> change a "private" attribute to public. Now all occurrences of the
> identifier need to be changed. If an assignment to the previously
> "private" attribute is missed, no warning will be issued (because
> Python allows new attributes to be added anywhere, even completely
> outside the class definition itself). And if the library is widely
> used, the probability of such bugs occurring is very high.

So _foo becomes foo. Then:

class X(object):
     def get_foo(self): return self._foo
     def set_foo(self, value): self._foo = value
     foo = property(get_foo, set_foo)

You have the public name "foo", for new usage outside the library, and the  
private name "_foo", for internal use only. And we're all happy. If it  
weren't for your "historical" reasons, using those trivial get/set  
functions is rather silly.

-- 
Gabriel Genellina




More information about the Python-list mailing list