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+ from enum import Enum
2+ from dataclasses import dataclass
3+
4+
5+ class OS (Enum ):
6+ UBUNTU = "Ubuntu"
7+ MAC = "Mac"
8+ WINDOWS = "Windows"
9+
10+
11+ @dataclass
12+ class Laptop :
13+ model : str
14+ os : OS
15+
16+
17+ @dataclass
18+ class Person :
19+ name : str
20+ age : int
21+ preferred_os : OS
22+
23+
24+ laptops = [
25+ Laptop ("Dell" , OS .UBUNTU ),
26+ Laptop ("MacBook" , OS .MAC ),
27+ Laptop ("HP" , OS .WINDOWS ),
28+ ]
29+
30+ person = Person ("Ali" , 22 , OS .UBUNTU )
31+
32+
33+ def match (laptops , person ):
34+ result = []
35+ for l in laptops :
36+ if l .os == person .preferred_os :
37+ result .append (l )
38+ return result
39+
40+
41+ print (match (laptops , person ))
Original file line number Diff line number Diff line change 1+ class Animal :
2+ def __init__ (self , name ):
3+ self .name = name
4+
5+ def speak (self ):
6+ return "some sound"
7+
8+
9+ class Dog (Animal ):
10+ def speak (self ):
11+ return "Woof!"
12+
13+
14+ class Cat (Animal ):
15+ def speak (self ):
16+ return "Meow!"
17+
18+
19+ dog = Dog ("Rex" )
20+ cat = Cat ("Milo" )
21+
22+ print (dog .name , dog .speak ())
23+ print (cat .name , cat .speak ())
You can’t perform that action at this time.
0 commit comments