The Dot Notation in Python

Dot Notation In Python

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 Mathers

Hopefully, 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.