Python widely uses built-ins (or module function) and not class methods.Solen([]) instead of [].length()filter(f, []) instead of [].filter(f)str(2) instead of 2.to_strsame for map, foreach etcThese prevent you from doing nice chaining which is possible in other languages like Ruby or Scala:(sorry very artificial example)my_list.map(f).filter(g).length()In Python you would need to either split into several lines:list_of_something_else = map(f, my_list)list_of_something_else_without_blah = filter(g, list_of_something_else)length = len(list_of_something_else_without_blah)In a single expression it doesn't look readable:len(filter(g, map(f, my_list))Is it considered to be not-Pythonic to chain methods?Or do people usually extend classes with a dozen of helper functions to make it easier?
Python methods vs builtin functions
python;functional programming
The Python functions are universally applicable; anything that has a __len__ method will work for len(), anything that's iterable will work for map(), etc.Another reason for len() to be a function is that it guarantees that the name is now the standard way of determining the length of any container; add-on types will have to implement __len__ to be compatible; if Python picked list.length() instead, other types could've implemented foo.get_length() or bar.len() or anything. Python followed the Principle of Least Astonishment instead here.And Guido van Rossum found len() to be more readable (I agree).And no, most of the Python stdlib doesn't support chaining; this can lead to overly-long lines that aren't very readable either. Instead, Python's syntax is generally powerful enough that you don't need much chaining.Your last expression would be better rewritten as:sum(1 for i in my_list if g(f(i)))as that'd save you having to create an intermediary list just to get the count of elements that were left after filtering.