Abstraction and Encapsulation:
Other Programming languages use private methods and variables which block the access of outsider classes. But in python there are no strict ways to private data but there are other ways so you can discourage people from accessing the part of the class by denoting that is an implementation detail, and should be used at your own risk.
There are two ways of data hiding in python
- Strongly Private Methods
- Weakly Private Methods
Strongly Private Methods are defined in python by using two double_underscores ‘__’ before the name of method let’s code this to get better understanding
Code:
class Private:
def __init__(self, name, id_number):
self.name = name
self.id_number = id_number
def __priv_method(self):
print("I am a Strongly Private Method")
def simp_method(self):
print("I am a Public Method!")
class Public(Private):
def __init__(self):
Private.__init__(self)
def call_method(self):
Private.simp_method(self)
def call_method_private(self):
Private.__priv_method(self)
obj1 = Private("Covid", 1902)
obj1.simp_method()
#This will raise an error
obj1.__priv_method()
As You can see here that when we call method simp_method this will run simply but when we try to call __priv_method this will generate an error because it is now hidden from other methods and when we try to call it in the main method it will generate error.
But you may be thinking that if we can’t access this method then why do we write it but wait we have a solution for accessing it. You can create a public method inside this class and call this private method inside this class. Let's code it for better understanding.
Code:
class Private:
def __init__(self):
pass
def __priv_method(self):
print("I am a Strongly Private Method")
def simp_method(self):
self.__priv_method()
obj1 = Private()
obj1.simp_method()
So now here you can see we access this method by using a public method that is simp_method in that case.
Weakly private methods use a single quote ‘_’ before the name of the method. It is just a convention; it does not stop the external code from accessing it. Let’s look at the code and see how it works
Code:
class Account:
def __init__(self):
self.username = "Covid"
self._balance = 50000
self.__password = "abcdefghi123456"
def show_info(self):
return f"Username: {self.username} \nBalance: {self._balance}\nPassword: {self.__password}"
# obj as an instance of class Account
obj = Account()
print("------- Accessing Attributes from Outside the Class ------------")
# username
print("Username:", obj.username)
# balance
print("Balance:", obj._balance)
# password - (obj._classname__attribute)
print("Password:", obj._Account__password)
In this program unlike a strongly private method we can access the data outside of the class but it is just to discourage people from accessing the part of the class by denoting that it is an implementation detail, and should be used at your own risk. Data hiding in python is confusing for developers who code in different programming languages. The Python way of encapsulation is a little different.
For comparative study and easy understanding..
Quote:i_am_public —---- no underscore
_i_am_protected —---- single underscore
__i_am_private —----- double underscores
That's it for today's thread See you in next thread
"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
Love me like you do