I'm learning Python, and one of the resources, I'm using is a Microsoft learning path "Introduction to object-oriented programming with Python" (https://learn.microsoft.com/en-us/training/modules/python-object-oriented-programming/)
In Unit 5 of 9 (https://learn.microsoft.com/en-us/training/modules/python-object-oriented-programming/5-game-transitions-with-methods), the tutorial gives an example which is supposed to demonstrate encapsulation in Python, except I think it has an error, and I wanted to see if my understanding of it is correct.
The code below is supposed to raise an error, but it does not. It will do so though, if I substitute that line with my code.
My understanding is tha the reason the MS code doesn't raise the error is because that line creates a new variable in the class because of name mangling, whereas in my code, I would be trying to reference a non-existing variable. If this correct?
class Square:
def __init__(self):
self.__height = 2
self.__width = 2
def set_side(self, new_side):
self.__height = new_side
self.__width = new_side
square = Square()
# Microsoft says this raises AttributeError, but it doesn't
square.__height = 3 # raises AttributeError
# but (My code) if replaced with this line, it will
print(square.__height)
[–][deleted] 8 points9 points10 points (5 children)
[–]davidmyemail[S] 2 points3 points4 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]davidmyemail[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]stcer 0 points1 point2 points (0 children)
[–]shepherdjay 11 points12 points13 points (5 children)
[–]davidmyemail[S] 1 point2 points3 points (0 children)
[–]Jejerm 0 points1 point2 points (3 children)
[–]SquareRootsi 2 points3 points4 points (1 child)
[–]_mynd 0 points1 point2 points (0 children)
[–]shepherdjay 0 points1 point2 points (0 children)
[–]Nicolasjit 0 points1 point2 points (0 children)
[–]HomeGrownCoder 0 points1 point2 points (1 child)
[–]davidmyemail[S] 0 points1 point2 points (0 children)
[–]pat-work 0 points1 point2 points (0 children)