16

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.

urz shah
  • 471
  • 2
  • 8
  • 18
  • See [here](http://docs.python.org/3/tutorial/classes.html?highlight=private#private-variables). – Marius Mar 26 '14 at 09:09
  • 5
    Because Python is not C# or Java. Why do those languages not have dynamic types and significant whitespace? Because they are not Python. – Daniel Roseman Mar 26 '14 at 09:17
  • 1
    @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. – Pablo Francisco Pérez Hidalgo Mar 26 '14 at 09:24
  • 2
    @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." – mallardz Jul 24 '14 at 17:54
  • @DanielRoseman On the other hand, your answer here: http://programmers.stackexchange.com/questions/91799/why-arent-there-explicit-access-modifiers-in-python was very informative! – mallardz Jul 24 '14 at 18:14

3 Answers3

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!

Community
  • 1
  • 1
BioGeek
  • 21,897
  • 23
  • 83
  • 145
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

  • 3
    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. –  Mar 26 '14 at 09:37
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.

geoff22873
  • 51
  • 4