File tree Expand file tree Collapse file tree 4 files changed +57
-10
lines changed
Expand file tree Collapse file tree 4 files changed +57
-10
lines changed Original file line number Diff line number Diff line change 11from typing import Protocol , TypeVar
22
3- T = TypeVar ("T" , bound = int | float | str )
3+ T = TypeVar ("T" , bound = int | float )
44
55
66class Adder (Protocol [T ]):
@@ -28,4 +28,4 @@ def add(adder: Adder) -> None:
2828
2929add (IntAdder ())
3030add (FloatAdder ())
31- add (StrAdder ())
31+ # add(StrAdder())
Original file line number Diff line number Diff line change 1- from typing import List , Protocol
1+ from typing import Protocol
22
33
44class ContentCreator (Protocol ):
55 def create_content (self ) -> str : ...
66
77
88class Blogger (ContentCreator , Protocol ):
9- posts : List [str ]
9+ posts : list [str ]
1010
1111 def add_post (self , title : str , content : str ) -> None : ...
1212
1313
1414class Vlogger (ContentCreator , Protocol ):
15- videos : List [str ]
15+ videos : list [str ]
1616
1717 def add_video (self , title : str , path : str ) -> None : ...
1818
Original file line number Diff line number Diff line change 33
44class LinkedListNode (Protocol ):
55 value : int
6- next : Optional ["LinkedListNode" ]
6+ next_node : Optional ["LinkedListNode" ]
77
88 def __str__ (self ) -> str :
9- return f"{ self .value } -> { self .next } "
9+ return f"{ self .value } -> { self .next_node } "
1010
1111
1212class Node :
13- def __init__ (self , value : int , next : Optional ["LinkedListNode" ] = None ):
13+ def __init__ (
14+ self ,
15+ value : int ,
16+ next_node : Optional ["LinkedListNode" ] = None ,
17+ ):
1418 self .value = value
15- self .next = next
19+ self .next_node = next_node
1620
1721 def __str__ (self ) -> str :
18- return f"{ self .value } -> { self .next } "
22+ return f"{ self .value } -> { self .next_node } "
1923
2024
2125def print_linked_list (start_node : LinkedListNode ):
Original file line number Diff line number Diff line change 1+ from math import pi
2+ from typing import Protocol , runtime_checkable
3+
4+
5+ @runtime_checkable
6+ class Shape (Protocol ):
7+ def get_area (self ) -> float : ...
8+
9+ def get_perimeter (self ) -> float : ...
10+
11+
12+ class Circle :
13+ def __init__ (self , radius ) -> None :
14+ self .radius = radius
15+
16+ def get_area (self ) -> float :
17+ return pi * self .radius ** 2
18+
19+ def get_perimeter (self ) -> float :
20+ return 2 * pi * self .radius
21+
22+
23+ class Square :
24+ def __init__ (self , side ) -> None :
25+ self .side = side
26+
27+ def get_area (self ) -> float :
28+ return self .side ** 2
29+
30+ def get_perimeter (self ) -> float :
31+ return 4 * self .side
32+
33+
34+ def print_shape_info (shape : Shape ):
35+ print (f"Area: { shape .get_area ()} " )
36+ print (f"Perimeter: { shape .get_perimeter ()} " )
37+
38+
39+ circle = Circle (10 )
40+ square = Square (5 )
41+
42+ print_shape_info (circle )
43+ print_shape_info (square )
You can’t perform that action at this time.
0 commit comments