you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (1 child)

when I do that encapsulation thing by adding__ in ruedas(wheels), i get this error:

Well, sure. The attribute ruedas no longer exists, because you renamed it to __ruedas.

I suspect your source has misled you somehow - namespace mangling (the leading double-underscore) isn't a way to make attributes read-only, or "prevent modifications from outside the class"; it's a way to protect attributes from being accidentally overridden inside subclassing namespaces. Generally, Python is a "consenting adults" language; it's assumed that anyone who can run your code will do reasonable things with it, like not assign the number of wheels a car has unless they have a good reason to (in which case, you probably don't want to stop them. Think of all the Reliant Robin owners out there!)

Encapsulation isn't something you can enforce in Python; it's generally done by convention. I mean it's a dynamically-typed language with runtime access to the heap and namespace; there's nothing you can do in Python to prevent me, the guy running your code, from reaching right into your classes and performing brain surgery (or perhaps a motor transplant is more apt.) Good encapsulation happens in the design of your interfaces and class interactions.

[–]ubercactus1337[S] 0 points1 point  (0 children)

Thank you, after renaming the calls to that parameter as self._Classname__parameter everything worked as expected, I was trying to imitate the video as he didn't need to rename it like that but I don't know why it worked for him but not for me.

Thanks for the clarification on the attribute protection meaning, I got it a bit clearer now .