Does Python have “private” variables in classes?

后端 未结 12 1980
不思量自难忘°
不思量自难忘° 2020-11-21 23:06

I\'m coming from the Java world and reading Bruce Eckels\' Python 3 Patterns, Recipes and Idioms.

While reading about classes, it goes on to say that in Py

12条回答
  •  野性不改
    2020-11-21 23:53

    "In java, we have been taught about public/private/protected variables"

    "Why is that not required in python?"

    For the same reason, it's not required in Java.

    You're free to use -- or not use private and protected.

    As a Python and Java programmer, I've found that private and protected are very, very important design concepts. But as a practical matter, in tens of thousands of lines of Java and Python, I've never actually used private or protected.

    Why not?

    Here's my question "protected from whom?"

    Other programmers on my team? They have the source. What does protected mean when they can change it?

    Other programmers on other teams? They work for the same company. They can -- with a phone call -- get the source.

    Clients? It's work-for-hire programming (generally). The clients (generally) own the code.

    So, who -- precisely -- am I protecting it from?

提交回复
热议问题