Let's Mangle

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?
Press + to interact
class Yo(object):
def __init__(self):
self.__honey = True
self.bro = True
print(Yo().bro)
print(Yo()._Yo__honey)
print(Yo().__honey) # Throws error

2.

Let’s try something symmetrical this time:

Press + to interact
class Yo(object):
def __init__(self):
# Let's try something symmetrical this time
self.__honey__ = True
self.bro = True
print(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:
Press + to interact
_A__variable = "Some value"
class A(object):
def some_func(self):
return __variable # not initialized anywhere yet
print(A().some_func())
print(A().__variable) # Throws an error

...

...