Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions python-double-underscore/cart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ShoppingCart:
def __init__(self, customer_id):
self.customer_id = customer_id
def __init__(self):
self.products = []

def add_product(self, product):
Expand All @@ -11,5 +10,3 @@ def get_products(self):

def __len__(self):
return len(self.products)

# Implementation...
2 changes: 0 additions & 2 deletions python-double-underscore/csv_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# csv_data.py

import csv


Expand Down
32 changes: 32 additions & 0 deletions python-double-underscore/mangling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class A:
def __init__(self):
self.__attr = 0

def __method(self):
print("A.__attr = ", self.__attr)


class B(A):
def __init__(self):
super().__init__()
self.__attr = 1 # Doesn't override A.__attr

def __method(self): # Doesn't override A.__method()
print("B.__attr = ", self.__attr)


if __name__ == "__main__":
a = A()
b = B()

# Call the mangled methods
print(f"{a._A__method()=}")
print(f"{b._B__method()=}")

# Check attributes
print(f"{a.__dict__=}")
print(f"{b.__dict__=}")

# Access the attributes on b
print(f"{b._A__attr=}")
print(f"{b._B__attr=}")