I read in python special class methods in Dive into Python , and it seems that some methods have odd or inconsistent syntax.
To get elements from a dictionary, you call the elements of a dictionary class method ()
>>> my_dictionary.items() [('name', 'Old Gregg'), ('Race', 'Scaly Man-Fish')]
However, to determine the number of keys in this dictionary, you must call len() and provide it as an argument as a dictionary.
>>> len(my_dictionary) 2
I always assumed that methods like len() were not really part of any class that you named them, given their syntax, but after reading Chapter 5, βDiving into Python,β I can see that len() actually leads to what the dictionary method is called.
my_dictionary.__len__()
So why is this not the case, and methods like this are called a typical class method?
my_dictionary.len()
Is there an agreement that I don't know about?
source share