urz shah urz shah - 1 year ago 105
Python Question

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.

Answer Source

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download