What are the advantages of having a python method, like lower(), being applied to an appropriate object, s, via the dot notation (i.e., s.lower()) versus a method, like len(), being also applied appropriately receiving the object as an argument, e.g. len(s) ? If I want to create such a method / function, what are the critical points I should consider choosing the first or the second implementation? Thanks!
Asked
Active
Viewed 37 times
0
pebox11
- 3,377
- 5
- 32
- 57
-
API consistency. Also see http://effbot.org/pyfaq/why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list.htm – Martijn Pieters Jun 21 '17 at 11:21
1 Answers
0
In my opinion, If the functionality should be part of an object behavior and it's related only to that object than implement it as a method at the class scope, for example, only str should have functions like lower.
When the function can be applied to a lot of class types like len can be applied on lists or on str than it should be a function.
omri_saadon
- 10,193
- 7
- 33
- 58