1
00:00:00,000 --> 00:00:03,920
A class defines a type with related data
and behavior.

2
00:00:04,000 --> 00:00:08,560
In our gene example, the data are a name,
a chromosome, and an expression

3
00:00:08,640 --> 00:00:09,440
value.

4
00:00:09,520 --> 00:00:14,400
An instance holds the state of one gene,
while a method uses that state to answer a

5
00:00:14,480 --> 00:00:15,360
question.

6
00:00:15,400 --> 00:00:19,680
The interview question is often about
references rather than syntax: which

7
00:00:19,880 --> 00:00:24,213
object changes, which objects compare
equal, and which object belongs in a

8
00:00:24,240 --> 00:00:25,520
visited set?

9
00:00:25,560 --> 00:00:28,880
We will work through those decisions with
the examples from the lesson.

10
00:00:29,200 --> 00:00:33,840
Call Gene with T P fifty three, chromosome
seventeen, and expression

11
00:00:34,000 --> 00:00:35,680
twenty three point five.

12
00:00:35,880 --> 00:00:38,800
Python normally creates the instance
through new.

13
00:00:38,840 --> 00:00:42,400
The initializer then assigns its
attributes through self.

14
00:00:42,480 --> 00:00:46,720
Self is the conventional parameter name
for the instance; it is not a Python

15
00:00:47,160 --> 00:00:47,653
keyword.

16
00:00:47,707 --> 00:00:52,640
The method call, t p fifty three dot is
expressed, supplies that instance

17
00:00:52,800 --> 00:00:53,787
automatically.

18
00:00:53,813 --> 00:00:57,920
The default threshold is one, so the
method returns true because twenty three

19
00:00:58,000 --> 00:01:00,080
point five is greater than one.

20
00:01:00,300 --> 00:01:05,153
The method signature names self and gives
threshold a default of one.

21
00:01:05,207 --> 00:01:09,740
The float and bool annotations describe
the expected argument and return types for

22
00:01:09,780 --> 00:01:11,047
a type checker.

23
00:01:11,073 --> 00:01:15,500
The docstring explains the threshold under
Args and the boolean result under

24
00:01:16,060 --> 00:01:16,700
Returns.

25
00:01:16,780 --> 00:01:21,060
Self is the supplied instance, so the Args
section does not repeat it.

26
00:01:21,100 --> 00:01:24,860
The return expression compares this
instance's expression attribute with the

27
00:01:24,940 --> 00:01:26,460
supplied threshold.

28
00:01:26,540 --> 00:01:29,980
Calling the method with a different
threshold changes the comparison, while

29
00:01:30,060 --> 00:01:33,100
the object's stored expression remains
unchanged.

30
00:01:33,333 --> 00:01:37,173
Start with one M Y C gene whose expression
is two.

31
00:01:37,373 --> 00:01:41,573
Assign gene alias to original gene, and
both names now refer to the same

32
00:01:41,653 --> 00:01:42,453
object.

33
00:01:42,533 --> 00:01:47,093
Changing expression through the alias to
nine also changes the value seen through

34
00:01:47,333 --> 00:01:48,333
original gene.

35
00:01:48,373 --> 00:01:52,613
A separate instance still has expression
two, even though it began with the same

36
00:01:52,773 --> 00:01:53,853
field values.

37
00:01:53,893 --> 00:01:57,733
Reassigning an alias to a different object
would change that name's reference

38
00:01:57,813 --> 00:01:58,853
instead.

39
00:01:59,013 --> 00:02:02,293
Assignment alone never asks Python to copy
this gene.

40
00:02:02,533 --> 00:02:07,093
A class attribute named events creates one
list for the class.

41
00:02:07,173 --> 00:02:11,333
Both run log instances find that list when
they have no events attribute of their

42
00:02:11,413 --> 00:02:12,080
own.

43
00:02:12,106 --> 00:02:15,493
An append through the first instance
therefore appears through the second

44
00:02:15,573 --> 00:02:16,880
instance too.

45
00:02:16,986 --> 00:02:20,933
Assigning a new list to the second
instance creates an instance attribute and

46
00:02:21,013 --> 00:02:23,200
leaves the class list unchanged.

47
00:02:23,226 --> 00:02:28,053
An initializer that assigns self dot
events to a new empty list gives each

48
00:02:28,133 --> 00:02:30,266
run its own list from the beginning.

49
00:02:30,320 --> 00:02:33,893
A shared immutable format name can stay on
the class.

50
00:02:34,133 --> 00:02:37,973
The ReadBatch property keeps the read
count a nonnegative Python

51
00:02:38,053 --> 00:02:39,213
integer.

52
00:02:39,253 --> 00:02:43,520
Its setter checks the value before
replacing the internal attribute.

53
00:02:43,546 --> 00:02:48,400
A valid update changes the count from one
hundred twenty to one hundred fifty.

54
00:02:48,426 --> 00:02:52,093
A rejected update leaves one hundred fifty
in place.

55
00:02:52,133 --> 00:02:56,613
Negative one raises ValueError; true and
two point five raise

56
00:02:57,173 --> 00:02:57,760
TypeError.

57
00:02:57,786 --> 00:03:02,293
The exact type check intentionally rejects
booleans, even though bool is an int

58
00:03:02,373 --> 00:03:03,573
subclass.

59
00:03:03,653 --> 00:03:06,880
Type annotations alone would not perform
these checks.

60
00:03:06,906 --> 00:03:11,333
The leading underscore marks an internal
attribute by convention, not access

61
00:03:11,773 --> 00:03:12,293
control.

62
00:03:12,533 --> 00:03:17,053
An instance method receives self and works
with one object's state.

63
00:03:17,093 --> 00:03:20,173
A class method receives cls instead.

64
00:03:20,213 --> 00:03:24,853
The from T S V factory parses a sample and
read count, then constructs an

65
00:03:25,093 --> 00:03:26,720
object through cls.

66
00:03:26,746 --> 00:03:30,613
An inherited factory can therefore
construct the subclass, provided its

67
00:03:30,693 --> 00:03:33,293
initializer accepts the same arguments.

68
00:03:33,333 --> 00:03:37,893
A static method receives neither self nor
cls automatically.

69
00:03:38,053 --> 00:03:41,120
Here parse count converts text to an
integer.

70
00:03:41,146 --> 00:03:45,413
A module level function could do the same
job; putting it on the class groups it

71
00:03:45,493 --> 00:03:47,013
with the related factory.

72
00:03:47,233 --> 00:03:51,073
The Sample dataclass declares its fields
and lets Python generate

73
00:03:51,273 --> 00:03:54,833
initialization, representation, and field
equality.

74
00:03:54,873 --> 00:03:58,273
It remains an ordinary class that can also
have methods.

75
00:03:58,313 --> 00:04:03,313
A default factory creates a new notes list
whenever a caller omits that argument.

76
00:04:03,393 --> 00:04:07,153
Our two SampleNotes objects therefore have
separate lists.

77
00:04:07,233 --> 00:04:11,633
Post initialization checks can reject an
empty sample label after the generated

78
00:04:11,713 --> 00:04:14,033
initializer assigns the fields.

79
00:04:14,113 --> 00:04:16,793
Those checks run during initialization
only.

80
00:04:16,833 --> 00:04:21,313
Later assignments do not call post
initialization again, and annotations do

81
00:04:21,433 --> 00:04:23,553
not validate values at runtime.

82
00:04:23,800 --> 00:04:28,520
Passing an existing list explicitly
bypasses the default factory.

83
00:04:28,600 --> 00:04:32,173
Both sample records then refer to that
same notes list.

84
00:04:32,227 --> 00:04:36,840
A shallow copy creates a new outer record
but keeps its reference to the original

85
00:04:36,920 --> 00:04:37,800
list.

86
00:04:37,960 --> 00:04:40,893
Appending reviewed appears in both
records.

87
00:04:40,947 --> 00:04:45,907
A deep copy made before the append has a
separate nested list in this example,

88
00:04:45,933 --> 00:04:48,360
so it still contains only received.

89
00:04:48,440 --> 00:04:52,787
Deep copying a large scientific array can
require substantial memory.

90
00:04:52,893 --> 00:04:57,000
Choose the sharing behavior deliberately
instead of assuming that any copy makes

91
00:04:57,080 --> 00:04:58,920
every object independent.

92
00:04:59,167 --> 00:05:03,327
Identity asks whether two names refer to
the same object.

93
00:05:03,447 --> 00:05:06,327
The is operator answers that question.

94
00:05:06,367 --> 00:05:09,727
Value equality uses the type's comparison
rule.

95
00:05:09,807 --> 00:05:13,887
Two separately created Gene objects
compare unequal because this class

96
00:05:13,967 --> 00:05:16,167
inherits identity equality.

97
00:05:16,207 --> 00:05:20,447
Two Sample instances of the same class
compare equal when all their fields

98
00:05:20,527 --> 00:05:24,167
match, because the dataclass generates
field comparison.

99
00:05:24,207 --> 00:05:27,847
Changing a field in one sample makes those
records unequal.

100
00:05:27,887 --> 00:05:32,607
Equal values therefore do not imply the
same object, and a class determines what

101
00:05:32,687 --> 00:05:34,207
equality means.

102
00:05:34,433 --> 00:05:38,153
A dictionary key or set member must be
hashable.

103
00:05:38,193 --> 00:05:42,913
Equal keys must have equal hashes, and the
hash must remain stable while the key is

104
00:05:42,993 --> 00:05:43,953
stored.

105
00:05:44,033 --> 00:05:48,313
Hash collisions are allowed, so matching
hashes do not prove equality.

106
00:05:48,353 --> 00:05:52,966
Our mutable Sample dataclass has no hash
and cannot enter a set.

107
00:05:53,020 --> 00:05:57,713
A frozen SampleKey with string study and
sample identifiers has compatible

108
00:05:57,793 --> 00:05:59,953
generated equality and hashing.

109
00:06:00,033 --> 00:06:02,713
Two equal keys retrieve the same count.

110
00:06:02,753 --> 00:06:07,033
Frozen prevents ordinary field assignment,
but it does not recursively freeze a

111
00:06:07,073 --> 00:06:09,953
contained list or make that list hashable.

112
00:06:10,200 --> 00:06:12,920
Inheritance specializes a type.

113
00:06:13,133 --> 00:06:17,800
DifferentialGene extends Gene with a fold
change and adjusted p value, while

114
00:06:17,920 --> 00:06:20,333
preserving the gene fields and behavior.

115
00:06:20,387 --> 00:06:24,680
The super call follows the method
resolution order; in this example the next

116
00:06:24,760 --> 00:06:26,787
initializer belongs to Gene.

117
00:06:26,813 --> 00:06:31,120
It does not always mean a fixed parent
when multiple inheritance is involved.

118
00:06:31,160 --> 00:06:35,040
Composition stores related objects inside
another object.

119
00:06:35,080 --> 00:06:39,880
A ComparisonResult contains one gene
reference, a comparison identifier, and a

120
00:06:39,960 --> 00:06:40,880
fold change.

121
00:06:40,920 --> 00:06:45,120
Two results can share the same gene while
describing different comparisons.

122
00:06:45,160 --> 00:06:49,160
A result contains a gene; it does not
specialize the Gene type.

123
00:06:49,400 --> 00:06:52,680
A SampleBatch stores several Sample
records.

124
00:06:52,800 --> 00:06:56,653
Its length method lets len report the
number of samples.

125
00:06:56,707 --> 00:07:01,560
The iterator method supplies an iterator
over those samples, so a for loop can

126
00:07:01,720 --> 00:07:02,573
visit them.

127
00:07:02,627 --> 00:07:07,080
A caller that sums read counts needs
iterable objects whose elements expose

128
00:07:07,160 --> 00:07:08,173
read count.

129
00:07:08,227 --> 00:07:11,853
It does not need every input to inherit
from SampleBatch.

130
00:07:11,907 --> 00:07:16,040
This is the practical role of Python
protocols and duck typing: define the

131
00:07:16,120 --> 00:07:20,360
operations the caller needs, then make
their behavior consistent.

132
00:07:20,440 --> 00:07:24,360
Type annotations can describe a narrower
contract for static checks.

133
00:07:24,600 --> 00:07:28,973
Two nodes can both store seven and still
be different objects.

134
00:07:29,107 --> 00:07:33,747
Following the first node to the second and
then to None does not form a cycle.

135
00:07:33,773 --> 00:07:38,640
A visited set must remember the nodes
themselves rather than just their values.

136
00:07:38,680 --> 00:07:43,640
The Node dataclass uses equality false to
retain object identity equality

137
00:07:43,720 --> 00:07:44,787
and hashing.

138
00:07:44,813 --> 00:07:49,240
A link back from the second node to the
first creates a cycle because traversal

139
00:07:49,280 --> 00:07:51,200
reaches the same object again.

140
00:07:51,240 --> 00:07:55,480
This visited set method uses expected
linear time and linear extra

141
00:07:55,560 --> 00:07:56,467
space.

142
00:07:56,493 --> 00:08:00,760
The linked list lesson also covers the two
reference method with constant extra

143
00:08:00,840 --> 00:08:01,720
space.

144
00:08:01,967 --> 00:08:05,967
Name the state that belongs to each
instance and identify any state

145
00:08:06,047 --> 00:08:08,634
intentionally shared by the class.

146
00:08:08,740 --> 00:08:13,087
State the rules that must hold, including
validation, equality, and hash

147
00:08:13,647 --> 00:08:14,314
behavior.

148
00:08:14,420 --> 00:08:19,087
Trace the references when a caller passes
a list, copies an object, or inserts a

149
00:08:19,167 --> 00:08:20,927
node into a set.

150
00:08:21,007 --> 00:08:24,740
Those explanations transfer directly to
interview questions.

151
00:08:24,794 --> 00:08:29,247
The complete lesson includes all class
definitions, Google style docstrings,

152
00:08:29,327 --> 00:08:32,247
exercises, and an AnnData example.

153
00:08:32,287 --> 00:08:36,847
AnnData adds another practical case:
distinguish a view from an explicit

154
00:08:37,127 --> 00:08:39,647
copy before you change scientific data.
