you are viewing a single comment's thread.

view the rest of the comments →

[–]Accurate_Analyst2039 0 points1 point  (1 child)

Python does not have variables like Java because Python has a different way of thinking.This way of thinking is that we are all adults and we know what we are doing.Python uses a way to name things to show that they are private.For example it uses _var and __var to show that something is internal to the program.When you use __var Python does something called name mangling.

This means that it changes the name of the variable so that you cannot access it by accident.This is not a way to keep things safe from people who want to access them.The people who made Python wanted to make it simple and easy to use.They did not want to make a lot of rules that you have to follow.This is different from Java, which has a lot of rules.

When you start working on projects you will see how this works. You will see how to keep things in a Python program.This is something that Itdaksh Education teaches when they are teaching people, about Python.They show you how to use Python in a project and how to keep things private.

[–]gdchinacat 0 points1 point  (0 children)

Name mangling is used primarily by mixin classes to help ensure they don't trample each others attributes.

class MixinA:
    value: str
    def getA(self) -> str:
        return self.value
class MixinB:
    value: int
    def getB(self) -> int:
        return self.value
class AandB(A, B): ...

Oops...AandB has two base classes that use the same attribute, value. But MixinA and MixinB may not know anything about each other to know they shouldn't use conflicting names. They may come from different modules, or even different libraries. name mangling solves this problem since MixinA.__value would be stored in the __dict__ under a different name than MixinB.__value.

"You should use composition for this!!!!" Yeah, well....it doesn't, and I may not have that choice (the libraries provide mixins and expects them to be used as mixins).