Introduction
A lot of people learn object-oriented programming with languages like C++ and Java. And while they learn, they’re told over and over again that encapsulation is one of the key principles of object-oriented paradigm and that they should take advantage of it.
In C++ and Java, things are pretty straight-forward. There are 3 magical and easy to remember access modifiers, that will do the job (public, protected and private). But there’s no such a thing in Python. That might be a little confusing at first, but it’s possible too. We’ll have look at how do it right in Python.
Python doesn’t have any mechanisms, that would effectively restrict you from accessing a variable or calling a member method. All of this is a matter of culture and convention.
Public
All member variables and methods are public by default in Python. So when you want to make your member public, you just do nothing. See the example below:
Let’s understand it using an example below-
>>> class employee: def __init__(self, name, sal): self.name=name self.salary=sal
You can access employee class’s attributes and also modify their values, as shown below.
>>>e1=Employee("Kiran",10000) >>> e1.salary 10000 >>> e1.salary=20000 >>> e1.salary 20000
Protected
Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses. How to accomplish this in Python? The answer is – by convention.
Python’s convention to make an instance variable protected is to add a prefix _ (single underscore) to it. This effectively prevents it to be accessed, unless it is from within a sub-class. See the example below:
>>> class employee: def __init__(self, name, sal): self._name=name # protected attribute self._salary=sal # protected attribute
This changes virtually nothing, you’ll still be able to access/modify the variable from outside the class. You can still perform the following operations:
>>> e1=employee("Swati", 10000) >>> e1._salary 10000 >>> e1._salary=20000 >>> e1._salary 20000
you explain politely to the person responsible for this, that the variable is protected and he should not access it or even worse, change it from outside the class
Hence, the responsible programmer would refrain from accessing and modifying instance variables prefixed with _ from outside its class.
Private
By declaring your data member private you mean, that nobody should be able to access it from outside the class, i.e. strong you can’t touch this policy. Any attempt to do so will result in an AttributeError.
>>> class employee: def __init__(self, name, sal): self.__name=name # private attribute self.__salary=sal # private attribute
Let’s try to access using private attribute directly –
>>> e1=employee("Bill",10000) >>> e1.__salary AttributeError: 'employee' object has no attribute '__salary'
As we can see that we’re getting AttributeError.
Python supports a technique called name mangling. Python performs name mangling of private variables. Every member with double underscore will be changed to _object._class__variable. If so required, it can still be accessed from outside the class, but the practice should be refrained.
>>> e1=Employee("Bill",10000) >>> e1._Employee__salary 10000 >>> e1._Employee__salary=20000 >>> e1._Employee__salary 20000
Let’s put everything altogether for better understanding
class Hello: def init(self, name): self.public_variable = 10 self.__private_variable = 30 def public_method(self): print(self.public_variable) print(self.__private_variable) print('public') self.__private_method() def __private_method(self): print('private')
Now let’s invoke these methods and variables-
>>> hello = Hello('name') >>> print(hello.public_variable) 10 >>> hello.public_method() 10 30 public private >>> print(hello.__private_variable) not allowed outside the class AttributeError: 'Hello' object has no attribute '__private_variable' >>> hello.__private_method not allowed outside the class AttributeError: 'Hello' object has no attribute '__private_method'
So bottom lines are-
- Default :- All members in a Python class are public by default.
- Public Member:- As per Python’s convention any variable which doesn’t contains double underscore as prefix is public member variable. Any member can be accessed from outside the class environment.
- Private variable:- Private variables means you can use them only inside the class. It’ll give you an error as soon as you use it or access it outside the class. But you can always use private member variable inside the class or any method of that class
- Private method:- Use double underscores as prefix in any method to make that private. Restrictions would be same on this like private variables.
- No IntelliSense:-You’ll not be able to see private methods in suggestion while accessing its class.Still if you access it then it’ll throw an error called “this method doesn’t exists”.
- you can use your private method inside the class using the self keyword like any member variable in class.