Skip to content

Commit bd8d069

Browse files
authored
Merge pull request #454 from realpython/python-double-underscore
Sample code for the article on underscores in Python names
2 parents 63235ed + 4fb2c71 commit bd8d069

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

python-double-underscore/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Single and Double Underscores in Python Names
2+
3+
This folder provides the code examples for the Real Python tutorial [Single and Double Underscores in Python Names](https://realpython.com/python-double-underscore/).

python-double-underscore/cart.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class ShoppingCart:
2+
def __init__(self, customer_id):
3+
self.customer_id = customer_id
4+
self.products = []
5+
6+
def add_product(self, product):
7+
self.products.append(product)
8+
9+
def get_products(self):
10+
return self.products
11+
12+
def __len__(self):
13+
return len(self.products)
14+
15+
# Implementation...

python-double-underscore/count.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
_count = 0
2+
3+
4+
def increment():
5+
global _count
6+
_count += 1
7+
8+
9+
def decrement():
10+
global _count
11+
_count -= 1
12+
13+
14+
def get_count():
15+
return _count
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# csv_data.py
2+
3+
import csv
4+
5+
6+
class CSVFileManager:
7+
def __init__(self, file_path):
8+
self.file_path = file_path
9+
10+
def read_csv(self):
11+
return self._read(delimiter=",")
12+
13+
def read_tsv(self):
14+
return self._read(delimiter="\t")
15+
16+
def _read(self, delimiter):
17+
with open(self.file_path, mode="r") as file:
18+
data = [row for row in csv.reader(file, delimiter=delimiter)]
19+
return data
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Passenger:
2+
def __init__(self, name, class_, seat):
3+
self.name = name
4+
self.class_ = class_
5+
self.seat = seat
6+
7+
# Implementation...

python-double-underscore/point.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Point:
2+
def __init__(self, x, y):
3+
self._x = x
4+
self._y = y
5+
6+
@property
7+
def x(self):
8+
return self._x
9+
10+
@x.setter
11+
def x(self, value):
12+
self._x = _validate(value)
13+
14+
@property
15+
def y(self):
16+
return self._y
17+
18+
@y.setter
19+
def y(self, value):
20+
self._y = _validate(value)
21+
22+
23+
def _validate(value):
24+
if not isinstance(value, int | float):
25+
raise ValueError("number expected")
26+
return value

python-double-underscore/shapes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
_PI = 3.14
2+
3+
4+
class Circle:
5+
def __init__(self, radius):
6+
self.radius = _validate(radius)
7+
8+
def calculate_area(self):
9+
return round(_PI * self.radius**2, 2)
10+
11+
12+
class Square:
13+
def __init__(self, side):
14+
self.side = _validate(side)
15+
16+
def calculate_area(self):
17+
return round(self.side**2, 2)
18+
19+
20+
def _validate(value):
21+
if not isinstance(value, int | float) or value <= 0:
22+
raise ValueError("positive number expected")
23+
return value

0 commit comments

Comments
 (0)