Parse the _ getitem _ proprietary method in Python and python _ getitem _

Source: Internet
Author: User

Parse the _ getitem _ proprietary method in Python and python _ getitem _

_ Getitem __
Let's look at a simple example to understand:

def __getitem__(self, key): return self.data[key]>>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3")>>> f{'name':'/music/_singles/kairo.mp3'}>>> f.__getitem__("name") '/music/_singles/kairo.mp3'>>> f["name"] '/music/_singles/kairo.mp3'

(1) The _ getitem _ special method is very simple. Like normal methods such as clear, keys, and values, it only redirects to the dictionary and returns the value of the dictionary. But how can we call it? Oh, you can call _ getitem __directly, but you will not actually do that: I will execute it here to tell you how it works. The correct method of using _ getitem _ is to let Python call it for you.
(2) It looks like the syntax you use to get a dictionary value. In fact, it returns the expected value. The following is a hidden link: Python has converted this syntax to f. _ getitem _ ("name") method call. This is why _ getitem _ is a dedicated class method, not only because you can call it yourself, but also use the correct syntax for Python to call it for you.

Slice slicing object
List has a magic slicing method:

>>> range(100)[5:10][5, 6, 7, 8, 9]

An error is reported for Fib. The reason is that the input parameter _ getitem _ () may be an int or slice object. Therefore, we need to make a judgment:

class Fib(object): def __getitem__(self, n): if isinstance(n, int): a, b = 1, 1 for x in range(n): a, b = b, a + b return a if isinstance(n, slice): start = n.start stop = n.stop a, b = 1, 1 L = [] for x in range(stop): if x >= start: L.append(a) a, b = b, a + b return L

Now try the Fib slice:

>>> f = Fib()>>> f[0:5][1, 1, 2, 3, 5]>>> f[:10][1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

However, the step parameter is not processed:

>>> f[:10:2][1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

The negative number is not processed. Therefore, there is still much work to do to implement a correct _ getitem.

In addition, if the object is treated as a dict, __getitem _ () parameter, it may also be an object that can be used as a key, such as str.

Corresponding to the _ setitem _ () method, the object is treated as list or dict to assign values to the set. Finally, there is a _ delitem _ () method to delete an element.

In short, through the above method, the class we define is similar to the list, tuple, and dict that comes with Python. This is entirely due to the "duck type" of the Dynamic Language ", you do not need to forcibly inherit an interface.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.