4 ms·
Ask HN: Python without dot notation
In his book Learn Python the Hard Way Zed Shaw writes:
You see that self in the parameters? You know
what that is? That’s right, it’s the “extra”
parameter that Python creates so you can type
a.some_function() and then it will translate
that to really be some_function(a).
Why use self? Your function has no idea what
you are calling any one “instance” of
TheThing or another, so you just use a generic
name self that way you can write your
function and it will always work.
http://learnpythonthehardway.org/static/LearnPythonTheHardWay.pdf (p. 116)
This seems to imply that the dot notation is an alternative notation. Is it possible to use Python without the dot notation?
- dalke 16y agoNo, because the text really should be that a.some_function() is transformed to a.__class__.some_function(a). There's no way to get to the function except through dots or circumlocutions like 'getattr'. There are some exceptions which make the above only an approximation. For example, if 'some_function' happens to be a function bound to the instance rather than the class. (def spam(): pass; instance.spam = spam; instance.spam() ) The result here is not the same as instance.__class__.spam(self) .