I'm learning Python, I've coded a bit in C++ before. I have simple very code:
class Car: def __init__(self, mark): self._mark = mark class Bus: def __init__(self): self.audi = Car("Audi") def display(self): print(self.audi._mark) def main(): bmw = Car("BMW") print(bmw._mark) reno = Bus() reno.display() main() As far as I know in C++, only classes that inherit have access to "protected" attributes. So my questions:
- What is the purpose of using "protected" attributes / methods in Python?
- Why do I have access to the attribute
_markfrom the main function/classBus?
Thank you for explain!
_thing is a convention that Python programmers generally respect, but it's not one that the runtime enforces._only gives a suggestion to the programmer that the attribute should not be modified outside of the class. And it has no effect on using in code?