Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 3.4: Encapsulation and Abstraction

0 Replies, 1808 Views

[Image: BFMMlbcQvml9HSqXcvNp]
Abstraction and Encapsulation:
It is not a really big concept of python but it is necessary to understand it. In this thread we will be discussing abstraction and encapsulation. Abstraction is dividing a task in small parts. We use encapsulation to achieve abstraction in Object Oriented Programming. Encapsulation means hiding implementation from the user. But it also means hiding data from other methods and classes that do not need change. Because if we have a complex project of programming and our program consists of many methods and these are interrelated to each other and we do not change our data accidently due to any of these methods then we use encapsulation. And to understand abstraction let’s suppose we want to create a game. We divide the task into many small tasks like game characters, Buildings, Cars etc and these are known as layers of abstraction.
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.

Idea 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 Heart
(This post was last modified: 05-27-2022, 06:52 PM by Covid-19.)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,608 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,646 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,826 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,592 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,775 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,805 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,801 05-26-2022, 08:50 PM
Last Post: Covid-19
  Tutorial Python 3.1: OOP Covid-19 2 1,629 05-24-2022, 06:37 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)