Tutorial Python 3.4: Encapsulation and Abstraction - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Python 3.4: Encapsulation and Abstraction (/showthread.php?tid=7943) |
Python 3.4: Encapsulation and Abstraction - Covid-19 - 05-27-2022 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 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: 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: 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: 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 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" |