Is there any difference between calling len([1,2,3]) or [1,2,3].__len__()?
If there is no visible difference, what is done differently behind the scenes?
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.
lenis a function to get the length of a collection. It works by calling an object’s__len__method.__something__attributes are special and usually more than meets the eye, and generally should not be called directly.It was decided at some point long ago getting the length of something should be a function and not a method code, reasoning that
len(a)‘s meaning would be clear to beginners buta.len()would not be as clear. When Python started__len__didn’t even exist andlenwas a special thing that worked with a few types of objects. Whether or not the situation this leaves us makes total sense, it’s here to stay.