05.05.21 17:18, Shreyan Avigyan пише:
> I don't know if it's worth adding private to python modules but what about 
> classes? Private in class is an important feature in OOP that Python lacks 
> (Most OOP languages have private like Java, C++, etc.). I don't know why it 
> was introduced in the first place but it's kind of a convention to have that 
> feature in OOP.

Private fields in Python are no less private than in C++. In C++ you can
read private fields:

    class User {
      public:
        string name;
      private:
        string password;
    };

    class HackedUser {
      public:
        string name;
        string password;
    };

    password = ((HackedUser*)&user)->password;

Of course it is a bad practice. You should not write this in your code,
and if your code allows executing arbitrary code in the same address
space, your security is already gone.

And the same is in Python. You can access private attributes, but
usually you should not (except for debugging purpose).

> Moreover why does private exist in the first place? If there's a reason then 
> shouldn't it apply to Python as well.

Private exists as a mean of self-limitation. It allows you to separate
your data and code on these which are purposed to be used outside of the
module or the class, and these which aren't. For your own good you must
follow this convention. The same reasons are applied to both C++ and
Python. It is more complicated in Java, but its security model is not
perfect in any case, and Python prefers the convenience of debugging and
simplicity of implementation.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6ITKZ45ZCUCWJVMPGVJJSDEXQ4TFZWQK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to