[Q] Accessors...
Gordon McMillan
gmcm at hypernet.com
Mon Jul 19 12:52:27 EDT 1999
Olivier Deckmyn writes:
>
> 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
You need to get used to Python's dynamism.
class A:
pass
a = A()
a.foo = 'bar'
If I've misunderstood your question, there was a recent thread
"Getters and Setters" about tricky ways to automatically create get
and set methods for attributes, so that given an instance of class A
with attribute foo, the user could write:
a = A()
a.getfoo()
without the programmer creating the getfoo method.
But if that's not what you want, don't go there!
- Gordon
More information about the Python-list
mailing list