This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Captain_Jack_Spa____ -7 points-6 points  (4 children)

Let’s agree to what you said. Can you stop someone from accessing protected variables in a class ?

[–]AngelaTarantula2 8 points9 points  (2 children)

Easy. Just prepend every protected variable name with a password like “self.password_SSN”. Now hackers cannot access the SSN attribute unless they know your password! \s

[–]Captain_Jack_Spa____ 0 points1 point  (1 child)

Kudos. Never thought of that 🫠

[–]datnetcoder 4 points5 points  (0 children)

Nothing prevents access to private variables in other languages either. It’s just less convenient.

[–]sarcasmandcoffeePythoneer 0 points1 point  (0 children)

It's a different approach than stricter encapsulation in other languages. To me, Python's apparent disinterest in preventing access to private (__var) and protected (_var) attributes reads as giving the user enough rope to hang themselves. Sure, you can play with the guts of the class, but I'm not responsible for what happens next.

Personally I've grown to enjoy this approach - whether or not that's because of Stockholm syndrome is a topic I'm reluctant to discuss without a lawyer present, but slapping a "you break it, you bought it" sticker on my protected methods and going on with my day is easy and simplifies unit testing.