Skip to content

Commit 815c4c6

Browse files
lpozobzaczynski
andauthored
Updating the underscore in Python names article (#688)
* Updating the underscore in Python names article * Post final QA --------- Co-authored-by: Bartosz Zaczyński <[email protected]>
1 parent 04972c3 commit 815c4c6

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

python-double-underscore/cart.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class ShoppingCart:
2-
def __init__(self, customer_id):
3-
self.customer_id = customer_id
2+
def __init__(self):
43
self.products = []
54

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

1211
def __len__(self):
1312
return len(self.products)
14-
15-
# Implementation...

python-double-underscore/csv_data.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# csv_data.py
2-
31
import csv
42

53

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class A:
2+
def __init__(self):
3+
self.__attr = 0
4+
5+
def __method(self):
6+
print("A.__attr = ", self.__attr)
7+
8+
9+
class B(A):
10+
def __init__(self):
11+
super().__init__()
12+
self.__attr = 1 # Doesn't override A.__attr
13+
14+
def __method(self): # Doesn't override A.__method()
15+
print("B.__attr = ", self.__attr)
16+
17+
18+
if __name__ == "__main__":
19+
a = A()
20+
b = B()
21+
22+
# Call the mangled methods
23+
print(f"{a._A__method()=}")
24+
print(f"{b._B__method()=}")
25+
26+
# Check attributes
27+
print(f"{a.__dict__=}")
28+
print(f"{b.__dict__=}")
29+
30+
# Access the attributes on b
31+
print(f"{b._A__attr=}")
32+
print(f"{b._B__attr=}")

0 commit comments

Comments
 (0)