Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why python does not have access modifier?And what are there alternatives in python?

why python does not have Access modifier like in c#, java i.e public, private etc.what are the alternative way of encapsulation and information hiding in python.

like image 954
urz shah Avatar asked Mar 26 '14 09:03

urz shah


People also ask

Why does Python not have access modifiers?

However, python can't be compiled and so sharing libraries necessitates sharing the source code. Thus, until someone creates a python compiler, access modifiers would not really achieve anything.

Which is the default access modifier used for 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.

How do you access protected methods in Python?

Protected variables are those data members of a class that can be accessed within the class and the classes derived from that class. In Python, there is no existence of “Public” instance variables. However, we use underscore '_' symbol to determine the access control of a data member in a class.

How do you make a protected variable in Python?

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.


Video Answer


2 Answers

From Wikipedia:

[Python] has limited support for private variables using name mangling. See the "Classes" section of the tutorial for details. Many Python users don't feel the need for private variables, though. The slogan "We're all consenting adults here" is used to describe this attitude. Some consider information hiding to be unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals. However, the strongest argument for name mangling is prevention of unpredictable breakage of programs: introducing a new public variable in a superclass can break subclasses if they don't use "private" variables.

From the tutorial: As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to "break into the definition."

The same sentiment is described in the We are all consenting adults paragraph of The Hitchhiker’s Guide to Python!

like image 76
BioGeek Avatar answered Oct 09 '22 06:10

BioGeek


The alternative is to name your "private" (they are not really private in python) with identifiers that make it easy to identify that those members should not be used from outside.

For example:

class RedmineWriter:

    __server = None
    __connected = False
...
...
...

However, if the class user really wants to change these attributes he will have no problem. It is his responsability not to do that.

Look at: http://docs.python.org/2/tutorial/classes.html#tut-private

like image 39
Pablo Francisco Pérez Hidalgo Avatar answered Oct 09 '22 06:10

Pablo Francisco Pérez Hidalgo