I know that python has a len() function that is used to determine the size of a string, but I was wondering why it’s not a method of the string object?
I know that python has a len() function that is used to determine the
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Strings do have a length method:
__len__()The protocol in Python is to implement this method on objects which have a length and use the built-in
len()function, which calls it for you, similar to the way you would implement__iter__()and use the built-initer()function (or have the method called behind the scenes for you) on objects which are iterable.See Emulating container types for more information.
Here’s a good read on the subject of protocols in Python: Python and the Principle of Least Astonishment