+ 6
How to make an attribute completely inaccessible?
I have created a class Rational that has protected attributes namely _num and _den. I've been trying to make _num and _den unavailable to the outside world. Further, restrict any other attribute to be created by the user from outside. The latter can be achieved using __slots__. But what to do for former?
3 Réponses
+ 3
Lisa there are a few ways that use descriptors or namedtuple etc. As I went through the source code of decimal module (_pydecimal.py) I found this implemented indeed.
The decimal object has attributes _exp, _int and _special which cannot be accessed from outside the class i.e., doing obj._int or obj._int = sth throws error.
I'm weak at OOP and so I could not understand these concepts (descriptors, namedtuple etc.) well. If someone here could help from outside the box I'll very thankful!
+ 1
Google "we are all consenting adults here" and "python".