Let's Mangle
We'll cover the following...
We'll cover the following...
Typically, we would discourage it, but for now, let’s mangle.
1.
Why does the following code throw an error?Python 3.5
class Yo(object):def __init__(self):self.__honey = Trueself.bro = Trueprint(Yo().bro)print(Yo()._Yo__honey)print(Yo().__honey) # Throws error
2.
Let’s try something symmetrical this time:
Python 3.5
class Yo(object):def __init__(self):# Let's try something symmetrical this timeself.__honey__ = Trueself.bro = Trueprint(Yo().bro)print(Yo()._Yo__honey__) # Throws an error
Why didn’t Yo()._Yo__honey__
work in the code above?
3.
Let's try something new:Python 3.5
_A__variable = "Some value"class A(object):def some_func(self):return __variable # not initialized anywhere yetprint(A().some_func())print(A().__variable) # Throws an error
...
...