__getitem__() in Python
In Python, there are a lot of functions that are used for particular purposes, and they have double underscores before and after the function name. They are called magic functions or dunder methods.
For example: __abs__ , __contains__ etc.
Some examples of magic methods are as follows:
This function is used to return the printable string.
This function is used to add the two objects or attributes.
This function is used to overload the greater than or equal operator. ( >=).
This function is called by the Math.floor() function internally.
This function is called by the Math.ceil() function internally.
This function is called by the Math.trunc() function internally.
This is the constructor for any class, which is called by the __new__() function.
__getitem__() is also a magic method in Python that is used in a class, and it gives the flexibility to any instance of the class to use the indexer operator.
So, it is used to evaluate the value of self[key] by any object or instance of the class. Let's suppose we have one object obj, then obj[key] will be equivalent to obj.__getitem(key).
We will some examples to understand the __getitem__() function:
In the above code, we have one custom class named JavatPoint. In this class, we have our own definition of the __getitem__() function where we print the some statement with the key.
Then we created its instance name myObj, and then we passed the numeric value 6000 as the key. It will print the statement and the value of the key. Then in the next line, we used a string as a key using an indexer, which will print the statement accordingly.
In the above code, we have one class named JavatPoint, where we have defined four functions. In the init function, we have taken a list. Then we have created an instance of the class with a list with four string values.
In the first statement, we have printed the value at 1st index so it will print the value and the statement of the __getitem__() function. Then we changed the value from string to integer and again called the indexer, so it printed again.
Then we deleted the first index value of the list using the __delitem__() function. So the value at the second index will be shifted to the left, and it will be printed as the first index.
In the above code, we have the class named Javatpoint, which accepts the object and it has the function __getitem__ () in which we pass a key or items, and in this function, we print the type of the key and the value of the key. Then we created the object or instance of the Javatpoint class named test and passed different types of keys into the test using indexer([ ]).
In the first example, we passed value 69, so it printed the data type as 'int' and its value.
The second example is of a type slice object, so it is printed as a class slice.
The third example is a statement, which has the datatype as str or string.
The fourth example is of a type tuple, and the fifth example is of a type slice.
The last example is of a type object, so it prints the value or the address of the object.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week