[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [GNUe-dev] Proposal for property access functions
From: |
John Lenton |
Subject: |
Re: [GNUe-dev] Proposal for property access functions |
Date: |
Thu, 31 Mar 2005 19:27:57 -0300 |
On Thu, 31 Mar 2005 23:49:18 +0200, Reinhard Mueller <address@hidden> wrote:
> Hi all,
>
> as we now require Python 2.3, we can use those cool features like
> property access functions.
actually, properties are available in Python 2.2:
http://www.python.org/2.2.3/descrintro.html#property
> Now, for the sake of maintainability, readability and consistence I
> suggest that we:
>
> * use names for properties that don't begine with an underscore (i.e.
> make the property "parent", not "_parent" for example)
>
> * use a private (e.g. "__bar") but otherwise identical variable name for
> instance variables hidden behind a property (e.g. "bar")
>
> * make accessor methods private methods and name them __set_prop and
> __get_prop; this makes sure they don't get mixed up with "normal"
> methods that already have names like "setCurrentResultSet" or
> "getRecordCount" and they are not called directly, so the difference
> between an instance variable and a property remains transparent.
>
> So, for example, a property "foo" would be implemented with the methods
> __get_foo and __set_foo and act on the hidden variable __foo.
one caveat and one suggestion: The caveat is that the following code,
class A(object):
def get_foo(self): return self.__foo
def set_foo(self, foo): self.__foo = foo
foo = property(get_foo, set_foo)
class B(A):
def set_foo(self, foo):
print "in B"
b = B()
b.foo = meh
might not work as expected. Using private accessors would reduce user
expectation as to how this might work, so it's a great idea.
The suggestion is that you only use this when you really need it. In
other words, don't use properties if a simple attribute will suffice.
--
John Lenton (address@hidden) -- Random fortune:
Don't anthropomorphise computers and cars, They hate that.