rage / programming-23

https://programming-23.mooc.fi/
64 stars 24 forks source link

[Question] Private variable/method in Python classes #4

Open UponTheSky opened 1 year ago

UponTheSky commented 1 year ago

I happened to discover the following while looking around this course materials.

At least in Part 9 and 10, the term "private" is widely used. It seems that Part 9 introduces the concept of "private" related to the concept of encapsulation.

But my question is, is that really a convention for the Python community to use mangling(__variable) to represent private variables and methods inside a class? According to this official docs regarding private variables mangling is used for avoiding naming collisions in class inheritance.

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. This can even be useful in special circumstances, such as in the debugger.

I am not a Python expert so I am not sure about the current standpoint of the community on this matter(and PEP8 seems a bit ambiguous). Could you recommend a few resources such as open source code examples? I'll really appreciate it.