Created
October 31, 2020 16:43
-
-
Save Nayemjaman/13eec5311c07042197d97fa025709327 to your computer and use it in GitHub Desktop.
A sample demonstration of __getitem__ in python class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" __getitem__ in class example """ | |
class Person: | |
def __init__(self): | |
# self.num_info = (1, 2, 3, 4, 5) | |
self.info = { | |
'name': 'Nayem Jaman', | |
'country': 'Bangladesh', | |
'mobile': 1234569 | |
} | |
def __getitem__(self, i): | |
return self.info[i] | |
person = Person() | |
print(person.info['name']) | |
# print(person[3]) | |
# utilizing __getitem__ method it can be written as | |
print(person['name']) | |
# print(person[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment