Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for Python class methods

Current License: CC BY-SA 2.5

10 events
when toggle format what by license comment
Feb 20, 2010 at 20:38 comment added Peter Hansen @AndiDog, you're missing his point still. From his point of view (and many others) there are in effect no variables in Python, just objects and names. The object can be accessed directly... the name is irrelevant, though it's what you're focusing on when you refer to "direct access of the variable". (Just trying to help clarify here... If I had downvoted, it would merely have been because your answer seems to imply strongly that this provides positive protection, when it really only protects against casual/accidental access.)
Feb 20, 2010 at 20:24 comment added AndiDog That's exactly what I said - the variable is named differently and thus cannot be accessed directly by the name __x. I don't know why you're still trying to correct me...
Feb 20, 2010 at 20:19 comment added Sunny Milenov You are making mistake to compare variable and its name. __x is just the name, not the variable itself. And the variable is accessible directly (just with another name), i.e. in Pyton you can not forbid this. While properties (or setters and getters) are a way to access a variable indirectly. In the languages, where "private" has meaning, one can not access "private" variables directly. You still can use some form of reflection, but it's not part of the language, but part of the framework, in which this language is used (Java, C#, etc., may use reflection trough framework functions).
Feb 20, 2010 at 20:15 comment added Max Shawabkeh My downvote was mainly for "I think it is merely a matter of style.". Python strongly suggests using directly exposed variables or properties rather than explicit getters and setters, and the naming convention is not a matter of taster here either; it is a standard way to denote private variables.
Feb 20, 2010 at 20:09 history edited AndiDog CC BY-SA 2.5
added 7 characters in body
Feb 20, 2010 at 20:08 comment added AndiDog Why the downvotes? I clearly wrote "cannot be accessed directly" which is completely true because with a instance variable "__x" you cannot access "instance.__x" but must use "instance._ClassName__x". It's about how you use such variables - as long as you don't access any variables called .__something (and instead use the property .something), you're good. Python doesn't enforce it but it should be a convention for every programmer.
Feb 20, 2010 at 20:04 comment added Tom You can use getters and setters, but Python has an entire mechanism to save you having to deal with them. Protection is built in. See "Accessors: Why Python Doesn’t Have Them":albert.infinitepigeons.org/wp/?p=135
Feb 20, 2010 at 19:57 comment added Devin Jeanpierre ""Private" instance variables that cannot be accessed except from inside an object, don’t exist in Python." (from the page you linked to)
Feb 20, 2010 at 19:52 comment added Sunny Milenov Actually - they can. Just with a different name, which includes the class name. The name of the variable is mangled, not its access level. From the same document you refer: "Note that the mangling rules are designed mostly to avoid accidents; it still is possible to access or modify a variable that is considered private."
Feb 20, 2010 at 19:48 history answered AndiDog CC BY-SA 2.5

AltStyle によって変換されたページ (->オリジナル) /