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

R

Ravi

Is it possible in python to create a property with the same name as
the member variable name of the class. e.g.

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

Please tell me how I can do so. Because if I do so, for the statement
at marker I get stack overflow for the assingm
 
C

Chris Rebert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top