You are currently viewing Attributes in python 3.9 : Private,Protected and Global
Attributes in python 3.9

Attributes in python 3.9 : Private,Protected and Global

Spread the love

These three attributes are the access modifiers which limit the access of the variables and functions of a class.

Attributes in python 3.9
Attributes in python 3.9

In the object-oriented languages access modifiers are used to limit the access to the variables and functions of a class. Most of the languages including python have three types of access modifiers, they are – global, private and protected.

Just like any other object-oriented programming language, access to variables or functions can also be limited in python using the access modifiers. Python makes the use of underscores to specify the access modifier for a specific data member and member function in a class.

Access modifiers play an important role to protect the data from unauthorized access as well as protecting it from getting manipulated. When inheritance is implemented there is a huge risk for the data to get destroyed or manipulated due to transfer of unwanted data from the parent class to the child class. Therefore, it is very important to provide the right access modifiers for different data members and member functions depending upon the requirements.

Protected Attributes in python 3.9.1

Protected members of a class can be accessed by other members

within the class and are also available to their subclasses.

No other entity can access these members. In order to do so, they can inherit the parent

class. Python has a unique convention to make a member Protected: Add a prefix _(single

underscore). This prevents its usage by outside entities unless it is a subclass.

For example:  In the last example if we want make the ‘horn’ attribute protected then we

have to just prefix it with single underscore: self._horn

Global Attributes in python 3.9.1

Global members of a class are available to everyone. So, they can be

accessed from outside the class and also by other classes too. All members of a class are by

default global in Python. These members can be accessed outside of the class, and their

values can be modified too.

For example:  if we define the horn attribute as self.horn. it will become a global attribute

and can be simply accessed.

Summary :

In this article we saw Attributes in python 3.9 : Private,Protected and Global so about this section you have any query then free to ask me

Name of Intern who share this Task :

rekha lodhi github link: https://github.com/Rekha5649

sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support(Pythonslearning), a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply