Skip to content

Commit 6427947

Browse files
authored
Merge branch 'master' into python-shebang
2 parents 8ec2b58 + 507db61 commit 6427947

File tree

9 files changed

+103
-0
lines changed

9 files changed

+103
-0
lines changed

python-mutable-immutable/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Mutable vs Immutable Types: What's the Difference?
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's Mutable vs Immutable Types: What's the Difference?](https://realpython.com/python-mutable-vs-immutable-types/).

python-mutable-immutable/color.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class Color:
6+
red: int
7+
green: int
8+
blue: int
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Immutable:
2+
def __init__(self, value):
3+
super().__setattr__("value", value)
4+
5+
def __setattr__(self, name, attr_value):
6+
raise AttributeError(f"can't set attribute '{name}'")
7+
8+
def __delattr__(self, name):
9+
raise AttributeError(f"can't delete attribute '{name}'")

python-mutable-immutable/mean.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def cumulative_mean(value, sample=[]):
2+
sample.append(value)
3+
return sum(sample) / len(sample)

python-mutable-immutable/person.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Person:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
6+
class Student(Person):
7+
def __init__(self, name, major):
8+
super().__init__(name)
9+
self.major = major
10+
11+
12+
john = Student("John", "Computer Science")
13+
print(type(john))
14+
john.__class__ = Person
15+
print(john.name)
16+
print(john.major)
17+
print(type(john))

python-mutable-immutable/point.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
# @property
11+
# def y(self):
12+
# return self._y
13+
14+
# def __repr__(self):
15+
# return f"{type(self).__name__}(x={self.x}, y={self.y})"
16+
17+
18+
class Coordinate:
19+
def __set_name__(self, owner, name):
20+
self._name = name
21+
22+
def __get__(self, instance, owner):
23+
return instance.__dict__[f"_{self._name}"]
24+
25+
def __set__(self, instance, value):
26+
raise AttributeError(f"can't set attribute {self._name}")
27+
28+
29+
class Point:
30+
x = Coordinate()
31+
y = Coordinate()
32+
33+
def __init__(self, x, y):
34+
self._x = x
35+
self._y = y
36+
37+
def __repr__(self):
38+
return f"{type(self).__name__}(x={self.x}, y={self.y})"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import math
2+
from collections import namedtuple
3+
4+
5+
class Point(namedtuple("Point", "x y")):
6+
__slots__ = ()
7+
8+
def distance(self, other: "Point") -> float:
9+
return math.dist((self.x, self.y), (other.x, other.y))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import math
2+
from typing import NamedTuple
3+
4+
5+
class Point(NamedTuple):
6+
x: float
7+
y: float
8+
9+
def distance(self, other: "Point") -> float:
10+
return math.dist((self.x, self.y), (other.x, other.y))

python-mutable-immutable/user.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class User:
2+
__slots__ = ("name", "job")
3+
4+
def __init__(self, name, job):
5+
self.name = name
6+
self.job = job

0 commit comments

Comments
 (0)