468
you are viewing a single comment's thread
view the rest of the comments
[-] fruitcantfly@programming.dev 2 points 5 days ago

A single underscore is just a naming convention, but double underscores triggers automatic name-mangling of the variable in question:

$ cat test.py
class foo:
        def __init__(self, x):
                self.__x = x

f = foo(1)
f.__x
$ python3 test.py
Traceback (most recent call last):
  File "/mnt/d/test.py", line 6, in <module>
    f.__x
AttributeError: 'foo' object has no attribute '__x'

However, much like private/protected variables in java, this is pretty trivial to circumvent if you want.

But I don't believe that you can argue that access modifiers are required for OO not to be shoehorned into a language, not when influential OO languages like Smalltalk didn't have this feature either. Java just happens to be closer to C++, where public/private/protected is much more rigidly enforced than either Java or Python

this post was submitted on 22 Jul 2025
468 points (100.0% liked)

Programmer Humor

25457 readers
1040 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS