File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Understanding Mixin Classes in Python
2+
3+ This folder contains sample code for the Real Python video course [ Understanding Mixin Classes in Python] ( https://realpython.com/courses/understanding-mixin-classes-in-python/ ) .
Original file line number Diff line number Diff line change 1+ class Person :
2+ def __init__ (self , name , age ):
3+ self .name = name
4+ self .age = age
5+
6+ def as_dict (self ):
7+ return {
8+ "name" : self .name ,
9+ "age" : self .age ,
10+ }
11+
12+
13+ class Car :
14+ def __init__ (self , make , model ):
15+ self .make = make
16+ self .model = model
17+
18+ def as_dict (self ):
19+ return {
20+ "make" : self .make ,
21+ "model" : self .model ,
22+ }
Original file line number Diff line number Diff line change 1+ class AsDictMixin :
2+ def as_dict (self ):
3+ return vars (self )
4+
5+
6+ class Person (AsDictMixin ):
7+ def __init__ (self , name , age ):
8+ self .name = name
9+ self .age = age
10+
11+
12+ john = Person ("John" , 42 )
13+ print (john .as_dict ())
14+
15+
16+ class Car (AsDictMixin ):
17+ def __init__ (self , make , model ):
18+ self .make = make
19+ self .model = model
20+
21+
22+ toyota = Car ("Toyota" , "Prius" )
23+ print (toyota .as_dict ())
Original file line number Diff line number Diff line change 1+ from abc import ABC , abstractmethod
2+
3+
4+ class Sequence (ABC ):
5+ @abstractmethod
6+ def __getitem__ (self , index ):
7+ pass
8+
9+ @abstractmethod
10+ def __len__ (self ):
11+ pass
Original file line number Diff line number Diff line change 1+ from abc import ABC , abstractmethod
2+
3+
4+ class Sequence (ABC ):
5+ @abstractmethod
6+ def __getitem__ (self , index ):
7+ pass
8+
9+ @abstractmethod
10+ def __len__ (self ):
11+ pass
12+
13+ def __iter__ (self ):
14+ index = 0
15+ while index < len (self ):
16+ yield self [index ]
17+ index += 1
18+
19+
20+ class MyRange (Sequence ):
21+ def __init__ (self , stop ):
22+ self .stop = stop # Assume that `stop` is >= 0.
23+
24+ def __getitem__ (self , index ):
25+ if 0 <= index < self .stop :
26+ return index
27+ raise IndexError
28+
29+ def __len__ (self ):
30+ return self .stop
31+
32+
33+ r = MyRange (10 )
34+ for number in r :
35+ print (number )
Original file line number Diff line number Diff line change 1+ from abc import ABC , abstractmethod
2+
3+
4+ class Sequence (ABC ):
5+ @abstractmethod
6+ def __getitem__ (self , index ):
7+ pass
8+
9+ @abstractmethod
10+ def __len__ (self ):
11+ pass
12+
13+ def __iter__ (self ):
14+ index = 0
15+ while index < len (self ):
16+ yield self [index ]
17+ index += 1
18+
19+
20+ class MyRange (Sequence ):
21+ def __init__ (self , stop ):
22+ self .stop = stop # Assume that `stop` is >= 0.
23+
24+ def __getitem__ (self , index ):
25+ if 0 <= index < self .stop :
26+ return index
27+ raise IndexError
28+
29+ def __len__ (self ):
30+ return self .stop
31+
32+
33+ r = MyRange (10 )
34+ for number in r :
35+ print (number )
Original file line number Diff line number Diff line change 1+ from abc import ABC , abstractmethod
2+
3+
4+ class IterMixin :
5+ # __iter__ works IF the class inheriting the mixin
6+ # already provides __getitem__ and __len__.
7+ def __iter__ (self ):
8+ print ("Hey from inside IterMixin.__iter__." )
9+ index = 0
10+ while index < len (self ):
11+ yield self [index ]
12+ index += 1
13+
14+
15+ class Sequence (IterMixin , ABC ):
16+ @abstractmethod
17+ def __getitem__ (self , index ):
18+ pass
19+
20+ @abstractmethod
21+ def __len__ (self ):
22+ pass
23+
24+
25+ class MyRange (Sequence ):
26+ def __init__ (self , stop ):
27+ self .stop = stop # Assume that `stop` is >= 0.
28+
29+ def __getitem__ (self , index ):
30+ if 0 <= index < self .stop :
31+ return index
32+ raise IndexError
33+
34+ def __len__ (self ):
35+ return self .stop
36+
37+
38+ r = MyRange (10 )
39+ for number in r :
40+ print (number )
You can’t perform that action at this time.
0 commit comments