The notion of "private" members in Python is simply a style convention. There's nothing in the language that enforces that. This is by design. Normally the "__" prefix designates that you should not directly access that member, usually because it's an implementation detail that could change and thus you shouldn't rely on it. But if you want to you are free to do so, understanding the consequences. Python is a dynamic language so things like monkey patching are possible, and sometimes used to powerful effect.
On 5/25/26 3:16 AM, WeiTung wrote: > Hello all: > > Until now we see if you have a class like this following in Python > > class MyClass: > __myPrivateMember = 0 > > def __myPrivateMethod(self): > # Here goes with what you want freely > pass > > mc = MyClass() > mc._MyClass__myPrivateMethod() > > Here we can still play with "__myPrivateMethod " by giving the > auto-generated prefix (this has been a public secret, you know), so it’s > also for the "__myPrivateMember ". > What I wanna say is, this isn't a good idea, because since it's > private, why could we still access it? We must abandon anyone to access it > anyway. > > Maybe we should generate some random string (such as address of > method/property?) or something like it? This means everytime when we run the > code, the "private" thing is random after compiling, only the Python system > itself know how to access it. > For those who directly access the "private" things, compiler SHOULD > give them an error, and when running , "AttributeNotFound" still occurs. > That's all my idea for good practice of programming in OOP for > Python. > > ----- > This email is signed by the free S/MIME certificate of “CerSign” > (https://www.cersign.com/smime-email-certificates.html) > > Be sure to import the root certificate of “CerSign Identity RSA Root” into > your trust root certificate list for encryption, download it from > https://www.cersign.com/root/rsa.html > > If you see “smime.p7s” in my letter, you can ignore it because your > mailbox doesn’t support S/MIME > > -- https://mail.python.org/mailman3//lists/python-list.python.org
