__getitem__ and __setitem__ in Python
Last Updated :
23 Mar, 2020
Dunder methods are double underscored methods that are used to emulate the behavior of built-in types. They are predefined methods that simplify many operations that can be performed on a class instance, like __init__(), __str__(), __call__() etc. These methods are very helpful because they are used in binary operations, assignment operations, unary and binary comparison operations.
Note: For more information, refer to
Dunder or magic methods in Python
__getitem__ and __setitem__
There are getter and setter methods as a part of these magical methods. They are implemented by
__getitem__()
and
__setitem__()
methods. But, these methods are used only in indexed attributes like arrays, dictionaries, lists e.t.c. Instead of directly accessing and manipulating class attributes, it provides such methods, so these attributes can be modified only by its own instances and thus implements abstraction.
Instead of making class attributes as public, these methods make them private, provide validation that only correct values are set to the attributes and the only correct caller has access to these attributes.
Let's take the example of a bank record of a person. It contains balance, transaction history, and other confidential records as part of it. Now, this bank record needs to be handled as a built-in data type to facilitate many operations. There are several methods which need access for balance and transaction history. If they directly modify the balance, they might end up inserting null values, or negative values that are very vulnerable. So, the
__getitem__()
and
__setitem__()
helps in presenting the details securely.
Example:
Python3 1==
class bank_record:
def __init__(self, name):
self.record = {
"name": name,
"balance": 100,
"transaction":[100]
}
def __getitem__(self, key):
return self.record[key]
def __setitem__(self, key, newvalue):
if key =="balance" and newvalue != None and newvalue>= 100:
self.record[key] += newvalue
elif key =="transaction" and newvalue != None:
self.record[key].append(newvalue)
def getBalance(self):
return self.__getitem__("balance")
def updateBalance(self, new_balance):
self.__setitem__("balance", new_balance)
self.__setitem__("transaction", new_balance)
def getTransactions(self):
return self.__getitem__("transaction")
def numTransactions(self):
return len(self.record["transaction"])
sam = bank_record("Sam")
print("The balance is : "+str(sam.getBalance()))
sam.updateBalance(200)
print("The new balance is : "+str(sam.getBalance()))
print("The no. of transactions are: "+str(sam.numTransactions()))
sam.updateBalance(300)
print("The new balance is : "+str(sam.getBalance()))
print("The no. of transactions are: "+str(sam.numTransactions()))
print("The transaction history is: "+ str(sam.getTransactions()))
Output
The balance is : 100
The new balance is : 300
The no. of transactions are: 2
The new balance is : 600
The no. of transactions are: 3
The transaction history is: [100, 200, 300]
Here you can see, how easy to implement
numTransactions()
,
getTransactions()
,
getBalance()
,
setBalance()
, just by implementing
__getitem__()
and
__setitem__()
methods. Also, it takes care of validation of balance and transaction history.
Similar Reads
Getter and Setter in Python In Python, a getter and setter are methods used to access and update the attributes of a class. These methods provide a way to define controlled access to the attributes of an object, thereby ensuring the integrity of the data. By default, attributes in Python can be accessed directly. However, this
4 min read
__getitem__() in Python __getitem__() is a special method (also known as a dunder or magic method) in Python that allows us to access an element from an object using square brackets, similar to how we access items in a list, tuple, or dictionary. It is commonly used to retrieve items from containers or objects that support
3 min read
__getslice__ in Python In python, Dunder Methods are those methods which have two prefixes and suffix underscores in the method name. They are also called Magic methods. Dunder means "Double Underscores". They are commonly used for operator overloading. These methods are not invoked directly by the user, but they are call
2 min read
Collections.UserDict in Python An unordered collection of data values that are used to store data values like a map is known as Dictionary in Python. Unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Note: For mo
2 min read
__file__ (A Special variable) in Python A double underscore variable in Python is usually referred to as a dunder. A dunder variable is a variable that Python has defined so that it can use it in a "Special way". This Special way depends on the variable that is being used. Note: For more information, refer to Dunder or magic methods in P
2 min read