News for hackers. Stuff that matters.
Re:I'm Switching to Python and Actually Liking It by wasabi991011 on Wednesday July 16, 2025 @02:02PM (#44585149)
What's the use case for __foo -> _ClassName__foo? I've used python a bunch but never this feature, so I'm curious.
Re:I'm Switching to Python and Actually Liking It by kstrauser on Wednesday July 16, 2025 @02:37PM (#44585480)
It makes it nearly impossible to reference that attribute by accident. Basically, if class Foo defines "self.__bar", the Python interpreter sees the leading "__" and mangles it. It's the lightweight equivalent of a private attribute in other languages, with the difference that Python will let you attempt to access it anyway. But if you do, and it makes your code utterly brittle and prone to explosion, no one will give you sympathy or help you fix it. They're much more likely to tell you not to do that.
Re:I'm Switching to Python and Actually Liking It by dragonwriter on Wednesday July 16, 2025 @03:00PM (#44585690)
It prevents accidentally overriding the member in subclasses which can prevent private implementation details of parent classes from being accidentally interfered with by child classes.

As composition over inheritance becomes more idiomatic, there are probably less places where this matters, but as long as inheritance is used at all it can be useful.

Re:I'm Switching to Python and Actually Liking It by antod on Wednesday July 16, 2025 @05:48PM (#44587155)
It's as close as they'll get to private methods while still having the "we're all consenting adults" philosophy that doesn't prevent you doing something dumb. Or if you use this and it breaks, you can't say you weren't warned.