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

3

17

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.

Infra answered 26/3, 2014 at 9:7 Comment(5)
See here.Provincial
Because Python is not C# or Java. Why do those languages not have dynamic types and significant whitespace? Because they are not Python.Subauricular
@DanielRoseman I Agree but I understand the shock it may produce for a person used to traditional-mainstream OO languages such a Java or C++. I was shocked when I started learning python.Galloglass
@DanielRoseman A question doesn't have to be an implicit criticism, otherwise what's the point of this site? The question is interesting to me to find out the reasoning behind the decisions to better understand programming languages and design. I imagine Guido had a better rationale when designing Python other than "it's not Java."Allele
@DanielRoseman On the other hand, your answer here: programmers.stackexchange.com/questions/91799/… was very informative!Allele
A
12

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!

Artemisa answered 26/3, 2014 at 9:10 Comment(0)
A
4

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

Aquitaine answered 26/3, 2014 at 9:12 Comment(1)
Double leading underscores induce name mangling, which is useful if you want subclasses to not deal with those members. If there are no subclasses, or they need to interact with those members (like protected in many static languages), a single leading underscore is more customary.Garrot
G
3

What differences do access modifiers in c# and java make? If I have the source code, I could simply change the access from private to public if I want to access a member variable. It is only when I have a compiled library that access modifiers can't be changed, and perhaps they provide some useful functionality there in restricting the API. 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.

Genuine answered 10/5, 2019 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.