Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Like other programming languages such as C#, JAVA, and C++, we don’t have access modifiers like public, private, and protected in python to define the access level restrictions for class members.

By default, all the python class members (variables, methods, etc.) are public. So, we can access all the class members outside of the class. To restrict access, you need to define the class members as private and protected.

To define the class variables or methods as private/protected, you need to prefix the single or double underscore (_) to the variable/method name.

Public Members in Python

As discussed by default, all the python class members are public, and we can access all the class members outside of the class based on our requirements.

Following is the example of creating a class with required data members and accessing those members outside of the class in python.

class user:
   id = 10
   name = "Suresh"

   def __init__(self, uid, uname):
     self.id = uid
     self.name = uname

   def userdetails(self):
     print("Id: {}, Name: {}".format(self.id, self.name))

u1 = user(2, "Rohini")
u1.id = 30
u1.name = "Trishi"
u1.userdetails()

When you execute the above python example, you will get the result as shown below.

Id: 30, Name: Trishi

Private Members in Python

Generally, the private members access is limited to the defined class. If you try to access the private members outside of the class, you will get an exception.

As discussed, we don’t have any built-in attributes to implement the private variables/methods in the python class. To create the private variables/methods in the python class, you need to prefix the double underscore (__) to the name of class variables or methods.

Following is the example of creating the private variables and methods in the python class.

class user:
   __id = 10
   __name = "Suresh"

   def __userdetails(self):
     print("Private Method")

u1 = user()
print(u1.__id)
print(u1.__name)
u1.__userdetails()

If you observe the above example, we prefixed double underscore (__) to the required variables and methods in the python class to define the private members.

When you execute the above python example, you will get the AttributeError exception as shown below.

AttributeError: 'user' object has no attribute '__id'
AttributeError: 'user' object has no attribute '__name'
AttributeError: 'user' object has no attribute '__userdetails'

Protected Members in Python

The protected members access limited to the defined class and the classes (sub-classes) that are derived from the defined class.

As discussed, in python we don’t have any built-in attributes to implement the protected variables/methods in the python class. To create the protected variables/methods in the python class, you need to prefix the single underscore (_) to the name of class variables or methods.

Following is the example of creating the protected variables and methods in the python class.

class user:

   def __init__(self, id, name):
     self._id = id
     self._name = name

   def _userdetails(self):
     print("Id: {}, Name: {}".format(self._id, self._name))

class Employees(user):
   pass

e1 = Employees(1, "Suresh")
print(e1._id)
print(e1._name)
e1._userdetails()

If you observe the above example, we prefixed a single underscore (_) to the required variables and methods in the python class to define the protected members. We are able to access the protected members from the derived classes.

The above example will return the result as shown below.

1
Suresh
Id: 1, Name: Suresh

This is how you can create the public, private, and protected variables, methods, etc., in python classes based on your requirements. 

A detailed view of how we can use access modifiers in Python

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Photo by Dayne Topkin on Unsplash

Object-oriented languages, like C++ and Java, use various keywords to control and restrict the resource usage of a class.

This is where keywords like public, privateand protected come into the picture. However, Python has a different way of providing the functionality of these access modifiers.

Public Keyword

public members of a class are available to everyone. So they can be accessed from outside the class and also by other classes too.

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Output of a public-access modifier

All members of a class are by default public in Python. These members can be accessed outside of the class, and their values can be modified too.

Protected Keyword

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.

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Output of protected-access modifier

However, this doesn’t fully perform the functionality of the protectedmodifier. The attribute defined in the above program is accessible outside the class scope. It can also be modified as well.

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Output of protected attribute modified

Private Keyword

The private members of a class are only accessible within the class. In Python, a private member can be defined by using a prefix __(double underscore).

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Output of private- access modifier

So, in the private modifier’s case, we cannot access the attribute. So is a private modifier the way ahead?

The answer would be No

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Python performs name mangling on private attributes. Every member with a double underscore will be changed to _object._class__variable.

Which access specifier is by default in class data member and member function private/public protected private and public in Python?

Output of private attribute modified

Therefore, this proves Python provides you with access-modifiers functionality, but it can’t be compared to classical languages like C++ and Java. This practice entirely depends upon the programmer.

So a responsible programmer, upon seeing an attribute with such a naming convention, would refrain from accessing it outside its scope. This also wouldn’t be good to use in cases where fellow programmers aren’t aware of such naming conventions.

Do you think that python should have the entire functionality of access modifiers like C++ & Java? or Do you think it's not that important?
Leave your experiences and thoughts below!

Which access specifier is by default in class data member and member function private/public protected private and public?

The default access for members and classes is private.

What is the default access specifier for the class members?

Class members, including nested classes and structs, can be public , protected internal , protected , internal , private protected , or private . Class and struct members, including nested classes and structs, have private access by default.

Which is the default access modifier used for the main () method in Python?

You can define the main method in your program without private, protected or, default (none) modifier, the program gets compiled without compilation errors.

Is there public private in Python?

There are three types of access modifiers in Python: public, private, and protected. Variables with the public access modifiers can be accessed anywhere inside or outside the class, the private variables can only be accessed inside the class, while protected variables can be accessed within the same package.