The Dot Notation In Python - AskPython
Maybe your like
Let’s discuss the dot notation in Python today. If you have even a little experience with coding in Python, or if you’ve been following our AskPython blog, you should have come across the term Object-Oriented Programming.
It is a programming paradigm based on the concept of real-world Objects. Each object has certain attributes that describe its state and methods that make them perform a certain task (equivalent to executing a function). Python is one such language.
In Python, almost every entity is traded as an Object. And knowing this is fundamental to grasping the significance of dot (.) notation.
What is the Dot Notation?
In simple words, the dot (.) notation is a way to access the attribute and methods of each method of instances of different object classes.
It is usually preceded by the object instance while the right end of the dot notation contains the attributes and methods.
Let’s create a class with multiple methods and then use the (.) notation to access those methods.
Creating your classes and objects:
class Person(): def __init__(self, name, age): self.name = name self.age = age def sayHello(self): print( "Hello, World" ) def sayName(self): print( f"My name is {self.name}") """ First, we create a Person Class that takes two parameters: name, and age. This is an object. The object currently contains two methods: sayHello() and sayName(). Now, we'll see how we can access those attributes and methods using dot notation from an instance of the class. """Now that our class is ready, we need to create an instance object.
#We create an instance of our Person Class to create an object. randomPerson = Person( "Marshall Mathers", 49) #Checking attributes through dot notation print( "Name of the person: " + randomPerson.name) print( "Age of the person: " + str(randomPerson.age) + "years" ) #Accessing the attributes through dot notation randomPerson.sayHello() randomPerson.sayName()In the last two lines, we’re accessing the methods within the class with the object of a class in the format <object name>.<method name>
Output:
Name of the person: Marshall Mathers Age of the person: 49 years Hello, World My name is Marshall MathersHopefully, the above example clears out your doubts regarding use of dot notation in Python.
Where else do we use the dot notation?
Any developer who has worked with Python has come across (.) notations. Here are some examples you must have come across in the past.
1. Index of a list
#A simple list called array with 3 elements words = ['godzilla', 'darkness', 'leaving heaven'] #Getting the index of the list words.index()2. Splitting a string
#A random string pun = "The movie Speed didn't have a director...Because if Speed had direction, it would have been called Velocity." #We use the split method to separate the string into two sections on either side of "..." print(pun.split("..."))These are some day-to-day examples of dot notation in action.
Conclusion
The dot notation is more than just a way to access inner methods. It’s a sophisticated technique to keep your code clean and to the minimum while ensuring complete functionality.
Tag » What Does Period Mean In Python
-
Why Are Functions Sometimes Called With Period, And ... - Reddit
-
Python Objects - Or What's With The Periods Everywhere?
-
In Python Coding, What If You Put A Period At The End Of A Statement?
-
Pandas.Period — Pandas 1.5.0 Documentation
-
What Is A Dot Operator In Python? - Tutorialspoint
-
Python | Pandas Period.hour - GeeksforGeeks
-
The Period Operator - Python - Learn Code Forum
-
What Does A Dot After An Integer Mean In Python? - Stack Overflow
-
What Does Period Mean In Python - DanieladdRusso
-
6. Expressions — Python 3.10.7 Documentation
-
%.2f In Python – What Does It Mean? - FreeCodeCamp
-
Understanding The Period/cycle Of Time Series Data - Cross Validated
-
- Time Period Checking