Friday, March 24, 2023
No Result
View All Result
Get the latest A.I News on A.I. Pulses
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
No Result
View All Result
Get the latest A.I News on A.I. Pulses
No Result
View All Result

Introduction to __getitem__: A Magic Methodology in Python

March 16, 2023
149 1
Home Data science
Share on FacebookShare on Twitter


Picture by Creator

 

 

Python is a magical language with many ideas that even superior customers might not be acquainted with. Dunder or Magical strategies is one among them. Magic strategies are particular strategies which are surrounded by double underscores. They don’t seem to be referred to as explicitly not like the bizarre strategies in python. One such magic technique is the __getitem__ technique, enabling the Python objects to behave like sequences or containers e.g lists, dictionaries, and tuples. It takes the index or slice and retrieves its related worth from the gathering. It’s invoked routinely at any time when we use the indexer [ ] operator to entry the weather inside your object.

Consider this technique as a magic wand that grants you the facility to extract the required info simply by writing a number of traces of code. Fascinating proper? This technique can be used extensively in information evaluation and machine studying. So, let’s dive deeper into the __getitem__ technique and uncover its energy and adaptability.

 

 

I would like you to know that your obligation as a Python programmer is extra than simply writing practical code. Your code needs to be environment friendly, readable, and maintainable. Utilizing __getitem__ will enable you obtain these objectives. Listed below are another advantages of utilizing this magic technique:

Reduces reminiscence utilization by permitting you to extract solely the important info as a substitute of loading the whole information construction into the reminiscence
Offers larger flexibility in how the info is dealt with and manipulated
Permits you to iterate over the gathering with out looping over the info
Enhances the performance by permitting you to write down superior indexing that might not be potential with the built-in varieties
Simplifies the code because it makes use of the acquainted notation

 

 

The syntax for the __getitem__ technique is as follows:

def __getitem__(self, index):
# Your Implementation
move

 

It defines the habits of the perform and takes the index that you’re making an attempt to entry in its parameter. We are able to use this technique like this:

 

This interprets to the assertion my_obj.__getitem__(index) beneath the hood. Now you would possibly assume that how is it totally different from the built-in indexer [] operator? Wherever you utilize this notation, python routinely calls the __getitem__ technique for you and is the shorthand for accessing parts. However if you wish to change the habits of indexing for customized objects, it is advisable to explicitly name the __getitem__ technique.

 

Instance #01

 

Allow us to begin with a straightforward instance first. We’ll create a Scholar class that can have the checklist of all the scholars and we are able to entry them by index and take into account that the index represents their distinctive pupil ID.

class Scholar:
def __init__(self, names):
self.names=names

def __getitem__(self,index):
return self.names[index]

section_A= Scholar([“David”, “Elsa”, “Qasim”])
print(section_A[2])

 

Output:

 

Now we’ll transfer over to a complicated instance the place we’ll change the indexing habits utilizing the __getitem__ technique. Suppose that I’ve a listing of string parts and I need to retrieve the factor at any time when I enter its index place and I may also get the index place if I enter the string itself.

class MyList:
def __init__(self, objects):
self.objects = objects

def __getitem__(self, index):
if isinstance(index, int):
return self.objects[index]
elif isinstance(index, str):
return self.objects.index(index)
else:
increase TypeError(“Invalid Argument Sort”)

my_list = MyList([‘red’, ‘blue’, ‘green’, ‘black’])

# Indexing with integer keys
print(my_list[0])
print(my_list[2])

# Indexing with string keys
print(my_list[‘red’])
print(my_list[‘green’])

 

Output:

 

 

This technique is extraordinarily helpful for the short lookup of the occasion attributes. Contemplating the flexibleness and flexibility of this technique, I’d say this is among the most underutilized magic strategies of Python. I hope you loved studying this text and do let me know within the remark part in case you are to know in regards to the different magic strategies in Python.  Kanwal Mehreen is an aspiring software program developer with a eager curiosity in information science and purposes of AI in drugs. Kanwal was chosen because the Google Technology Scholar 2022 for the APAC area. Kanwal likes to share technical information by writing articles on trending matters, and is captivated with enhancing the illustration of ladies in tech business. 



Source link

Tags: __getitem__IntroductionMagicMethodPython
Next Post

How To Use GPT-4: Options, Use Circumstances, And Extra

Enhancing the understanding of metal-organic frameworks

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent News

Optimize Knowledge Warehouse Storage with Views and Tables | by Madison Schott | Mar, 2023

March 24, 2023

Bard Makes use of Gmail Information | Is AI Coaching With Private Information Moral?

March 24, 2023

Key Methods to Develop AI Software program Value-Successfully

March 24, 2023

Visible language maps for robotic navigation – Google AI Weblog

March 24, 2023

Unlock Your Potential with This FREE DevOps Crash Course

March 24, 2023

High 15 YouTube Channels to Degree Up Your Machine Studying Expertise

March 23, 2023

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
A.I. Pulses

Get The Latest A.I. News on A.I.Pulses.com.
Machine learning, Computer Vision, A.I. Startups, Robotics News and more.

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
No Result
View All Result

Recent News

  • Optimize Knowledge Warehouse Storage with Views and Tables | by Madison Schott | Mar, 2023
  • Bard Makes use of Gmail Information | Is AI Coaching With Private Information Moral?
  • Key Methods to Develop AI Software program Value-Successfully
  • Home
  • DMCA
  • Disclaimer
  • Cookie Privacy Policy
  • Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In