-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_first_look_test.py
More file actions
121 lines (82 loc) · 3 KB
/
a_first_look_test.py
File metadata and controls
121 lines (82 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""A First Look at Classes."""
from typing import Generic, List, TypeVar
import pytest
class Greeting:
"""A simple example class."""
def __init__(self, message: str) -> None:
"""Customize an instance to a specific initial state."""
self.msg = message
def greet(self) -> str:
return self.msg
def test_data_attribute() -> None:
"""Data attributes."""
greeting = Greeting("hello")
greeting.counter = 1
greeting.counter += 1
assert greeting.counter == 2
del greeting.counter
with pytest.raises(
AttributeError, match="'Greeting' object has no attribute 'counter'"
):
print(greeting.counter)
def test_method_object() -> None:
"""Method objects."""
greeting = Greeting("hello")
# Store method object to be called at a later time.
method_obj = greeting.greet
assert method_obj() == "hello"
assert greeting.greet() == Greeting.greet(greeting)
class Dog:
shared_tricks: List[str] = [] # Mistaken use of a class variable
def __init__(self, name: str) -> None:
self.name = name
self.tricks: List[str] = []
def add_shared_trick(self, trick: str) -> None:
"""Add trick to class variable - mistake."""
self.shared_tricks.append(trick)
def add_trick(self, trick: str) -> None:
"""Add trick to instance variable."""
self.tricks.append(trick)
def test_class_instance_variables() -> None:
"""Class and instance variables."""
fido = Dog("Fido")
dido = Dog("Dido")
fido.add_shared_trick("roll over")
dido.add_shared_trick("play dead")
assert fido.shared_tricks == dido.shared_tricks == ["roll over", "play dead"]
fido.add_trick("roll over")
dido.add_trick("play dead")
assert fido.tricks == ["roll over"]
assert dido.tricks == ["play dead"]
def add_power(self: Dog, power: str) -> None:
"""Define a function outside the class."""
if not hasattr(self, "power"):
self.powers = []
self.powers.append(power)
def test_add_function_to_class() -> None:
"""Assign a function object to a local variable in a class."""
Dog.add_power = add_power
fido = Dog("Fido")
fido.add_power("drool")
assert fido.powers == ["drool"]
with pytest.raises(
AttributeError, match="type object 'Dog' has no attribute 'powers'"
):
# `powers` is an instance variable, not a class variable.
print(Dog.powers)
T = TypeVar("T")
class Bag(Generic[T]):
def __init__(self) -> None:
self.data: List[T] = []
def add(self, value: T) -> None:
"""Add a value to the bag."""
self.data.append(value)
def addtwice(self, value: T) -> None:
"""Add a value to the bag, twice, by using `self`'s method attribute."""
self.add(value)
self.add(value)
def test_calling_other_methods() -> None:
"""Calling other methods by using method attributes of the `self` argument."""
bag = Bag[str]()
bag.addtwice("apple")
assert bag.data == ["apple", "apple"]