diff --git a/python-double-underscore/cart.py b/python-double-underscore/cart.py index 4ccd2bf5a4..91648a3dc2 100644 --- a/python-double-underscore/cart.py +++ b/python-double-underscore/cart.py @@ -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): @@ -11,5 +10,3 @@ def get_products(self): def __len__(self): return len(self.products) - - # Implementation... diff --git a/python-double-underscore/csv_data.py b/python-double-underscore/csv_data.py index f7f27fdc27..514197efa2 100644 --- a/python-double-underscore/csv_data.py +++ b/python-double-underscore/csv_data.py @@ -1,5 +1,3 @@ -# csv_data.py - import csv diff --git a/python-double-underscore/mangling.py b/python-double-underscore/mangling.py new file mode 100644 index 0000000000..74ed7cbd8e --- /dev/null +++ b/python-double-underscore/mangling.py @@ -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=}")