It is time to talk about examples, this time we are talking about Python dedicated class method used by the example tutorial, there are a lot of OH.
1.__getitem__ Special method
1 >>> map = {' name ': ' C little plus '}2 >>> map.__getitem__ ("name") 3 ' c\xe5\xb0\x8f\xe5\x8a\xa0 '
Just redirect to the dictionary, return the value of the dictionary
2.__setitem__ Special method
>>> map = {' name ': ' C little plus '}2 >>> map.__setitem__ (' Monaker ', ' Team Flower ') 3 >>> map4 {' monaker ': ' \xe9\ X98\x9f\xe8\x8a\xb1 ', ' name ': ' c\xe5\xb0\x8f\xe5\x8a\xa0 '}
Map is actually a class, it is as if as a dictionary, like C small plus, as much as possible as a team flower, although he is a man.
>>> map[' monaker ' = ' Team flower ' >>> map{' monaker ': ' \xe9\x98\x9f\xe8\x8a\xb1 ', ' name ': ' C\xe5\xb0\x8f\xe5\ X8a\xa0 '} This looks like a normal dictionary syntax, and this line of code actually calls the map.__setitem__ (' Monaker ', ' Team Flower ') secretly
Overriding the __setitem__ method in a class is defined strictly in the same form as the parent class method. The name of the argument doesn't matter, just the number. While calling the parent class directly, there is no __setitem__. But Python walks along the parent tree, knowing that the class that we are calling the method is found.
3 . __repr__ is a dedicated method that is called when repr (instance) is called. The REPR function is a built-in function that returns a string representation of an object. It can be used on any object, not just an instance of the class. You've been quite familiar with repr, even though you don't know it. In the interactive window, when you just typed in a variable name, and then press Enter,python to use REPR to display the value of the variable. Use some data to create a dictionary d, and then use print repr (d) to take a look.
4.__cmp__ is called when comparing class instances. Typically, you can compare any two Python objects by using = =, not just class instances. There are rules that define when built-in data types are considered equal, for example, dictionaries are equal when they have all the same keywords and values. For class instances, you can define the __cmp__ method, write the comparison logic yourself, and then you can use = = to compare your classes, and Python will call your __cmp__ dedicated method for you.
5.__len__ is called when it calls Len (instance). Len is a built-in function that can return the length of an object. It can be used for any object that is supposed to have a length. The Len of the string is the number of its characters, and the Len of the dictionary is the number of its keywords, and the Len of the list or sequence is the number of elements. For class instances, define the __len__ method, and then write the length of the calculation, and then call Len (instance), Python will call your __len__ dedicated method for you.
6.__delitem__ called del Instance[key], you may remember it as a way to remove a single element from the dictionary. When you use Del in a class instance, Python calls the __delitem__ private method for you.
A dedicated method means that any class can hold key-value pairs like a dictionary, as long as the __setitem__ method is defined. Any class can behave like a sequence, as long as the __getitem__ method is defined. Any class that defines the __cmp__ method can be compared by = =. And if your class behaves like something with a similar length, do not define the GetLength method, but define the __len__ method and use Len (instance).
"Recommended"
1. Learn more about the special function __len__ (self) in Python
2. Required knowledge--python Len Example
3. Summarize the use instances of the Len () function in Python
4. Python Magic method __getitem__, __setitem__, __delitem__, __len__ respectively introduced