Python syntax inconsistency?

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?

+6
source share
3 answers

Guido van Rossum explained this as follows :

(a) For some operations, prefix notation only reads better than postfix - prefix (and infix!) operations have a long tradition in mathematics that loves notes where visual effects help the mathematician think about a problem. Compare the simplicity with which we rewrite a formula, such as x * (a + b), in xa + xb, to awkward the same action using raw OO notation.

(b) When I read the code that says len(x) , I know that it is asking for the length of something. This tells me two things: the result is an integer, and the argument is some kind of container. On the contrary, when I read x.len() , I should already know that x is some kind of container that implements an interface or inherits from a class with standard len() . Witness the confusion we sometimes experience when a class that does not implement a mapping has a get() or keys() method or something that is not a file has a write() method.

+12
source

This distinction is useful because it separates use from source. When encoding an extension into a dictionary class, you know that you are redefining what is embedded in the base object as part of the language. If you really want to change the way the container behaves, you overwrite __len__(), __getitem__() , etc.

However, as a user of some code, I have nothing to worry about. If I use len(my_dictionary) , I expect consistent behavior based on a programmer who knows the correct way to implement __len__() . While anyone can write the random method my_dictionary.len (). I don’t know about that.

Link http://www.siafoo.net/article/57 for more reference information on underlining methods, mainly related to your current issue (section 2.7.1 for __len__() ).

+1
source

We are not in the Java world.

There are many methods in Python that apply to an object if they implement a related API.

 len() --> __len__() str() --> __str__() repr() --> __repr__() 

Moreover:

Why

 my_dictionary.len() 

- method?

In Javascript objects, they expose their size through the length attribute, which is more natural than using the method ... sorry, but not all stupidity coming from the Java world is good and should be available in other languages.

0
source

Source: https://habr.com/ru/post/887333/


All Articles