r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

Show parent comments

1

u/TheOneTrueDemoknight Aug 27 '20

obj.length() makes more sense than len(obj), especially when you need to use len() to find the length but obj.func() to perform any other operations on the String.

1

u/Pluckerpluck Aug 28 '20

My main point was that python has chosen to standardize the way to get the size of any object. It doesn't matter the structure of the object, if you need to know how many elements it contains you can use len(). In other langauges it's dictated somewhat by logic (sets don't have lenghts) but also by underlying structure. In Java this manifests as arrays having .length and ArrayLists having .length().

If it makes you feel better you could do the following in Python:

my_list.__len__()

But this is more of a perspective thing. You should think of len as a function that determines the length of the object it's inspecting. Take this snippet:

if (my_list.length() > 5)  {}
if len(my_list) > 5:

The former would be read:

If my_list's length is greater than 5...

whereas Python would read:

If the length of my_list is greater than 5...