开发者

Why does Python use built-ins instead of object methods (like ruby, etc)? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why does python use 'magic methods&开发者_运维问答#39;?

Just want to clarify, this is NOT a python vs ruby. I've been a user of python for ~1 year. I like it a lot except for some of the funky built-ins vs having methods. Like why do you have to do len(listA) instead of listA.length() or listA.size() like ruby or Java. From my Java background it seems intuitive enough to have listA.length() instead of len (it's easy in an ide, etc to find out methods which an object has rather than learning the built-ins). Could anyone please explain this reason for this design choice?


Mainly due to history. Python's ancestor was a teaching language called ABC, which was, I gather, somewhat BASIC-like.

Personally, I think it's kind of nice to know that I can always get the length of something with len(thing) without having to know what the implementer of a particular type called their equivalent method: len()? length()? getLength()? You can have all the conventions you like, but when the len() built-in function breaks on your object because you didn't implement __len__(), you can be sure everyone will name their length method __len__().

Your point about IDEs auto-completing methods applies only if the IDE knows what type of object a given name is bound to, which is not always possible in a dynamic language like Python. At least, not without executing the code.

BTW, some of what you think are built-in functions (such as str(), int(), and float()) are not functions but types. (In other words, str(42) is a string construction, not a function call that returns a string.) These are perfectly cromulent even if you're an OO stickler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜