2

Python's length of list can be calculated by len(a_list). My question is, why was a_list.len() not implemented? Is there a reason behind it?

NullException
  • 4,232
  • 8
  • 24
  • 44
  • 1
    because the people writing the language decided it would not add any value to the language ... – Joran Beasley Feb 27 '14 at 20:12
  • It's asking something about the language, which might be more appropriate on programmers. – octopusgrabbus Feb 27 '14 at 20:14
  • 7
    they have a `list.__len__` method, which `len(list)` calls – mhlester Feb 27 '14 at 20:14
  • a_list.len() looks more intuitive to me than len(a_list), hence the question. Previous answers, which this question is sighted as duplicated, does not answer the reason other than the no value in adding extra len() method. – NullException Feb 27 '14 at 20:18
  • 2
    There's also an official [FAQ entry](http://docs.python.org/3/faq/design.html#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list) on this. –  Feb 27 '14 at 20:19
  • As far s the user is concerned it is best to have a standard operation that handles everything and the individual type processing is encapsulated. – sabbahillel Feb 27 '14 at 20:20
  • `from forbiddenfruit import curse;curse(list,"len",len)` is a neat trick/hack if you really want to be able to do this (`[1,2,3].len()`) @dt369 (youll need https://github.com/clarete/forbiddenfruit ) – Joran Beasley Feb 27 '14 at 20:34
  • 1
    In the artical [why does python use methods for some functionality eg list index but functions for other eg len list](http://effbot.org/pyfaq/why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list.htm) a good reasoning is given: _some operators read better a prefix then postfix like len()._ – eremmel Nov 01 '17 at 09:21

1 Answers1

3

likely just because its redundant when we already have and are use to len() maybe you could add a .len() method :)

there is a a_list.__len__()