[Q] Accessors...

Olivier Deckmyn olivier.deckmyn at mail.dotcom.fr
Mon Jul 19 10:08:17 EDT 1999


HI again...

Is there a way to do the following :

class MyClass :
    def __init__(self):
        self._myAttr = None

    def get_myAttr(self):
        return self._myAttr

    def set_myAttr(self, value)
        self._myAttr = value # might do more interresting checkings for
examples


and then use this like that:

m=MyClass()
m.myAttr="foo"
print m.myAttr

This way, because it exists two methods get_XXX and set_XXX, a special
proporty XXX would be implicitly created, and would make the user call
"automatically" the get/set methods.

(Please note that myAttr is defined nowhere, but is the "guessed" word found
after methods get_XXX and set_XXX)

I other words, would be AUTOMATICALLY "understood" by the interpreter as :

m=MyClass()
m.set_myAttr("foo")
print m.get_myAttr()

This is what I call "accessors" : a way to make the user use automatically
methods to access variables ?
Such a thing is a "property" in Delphi for example.


Thanx all for your support.






More information about the Python-list mailing list