Make a python property with the same name as the class member name

Chris Rebert clp2 at rebertia.com
Fri Feb 27 11:43:15 EST 2009


On Fri, Feb 27, 2009 at 5:59 AM, Ravi <ra.ravi.rav at gmail.com> wrote:
> Is it possible in python to create a property with the same name as
> the member variable name of the class. e.g.

No, because accessing the property and the instance variable are
*syntactically identical* (it's the raison detre of properties) so
there's no way for Python to know which one you mean when.
Typically, people name the variable used internally by the property as
an underscore followed by the property name (e.g. self._i).

> Class X:
>    ...
>    self.i = 10 # marker
>    ...
>    property(fget = get_i, fset = set_i)

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list