Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

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 their 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 grasp 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. Length of a list

#A simple list called array with 3 elements
array = ['godzilla', 'darkness', 'leaving heaven']

#Getting the length of an array
array.len()

First, we notice that a list object has been created with 3 values inside it. Now we can speculate further than len() is a built-in method inside the list class that returns the number of elements in our list. The len() method is accessed via dot notation.

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 "..."
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.