-
Notifications
You must be signed in to change notification settings - Fork 676
Expand file tree
/
Copy pathcode.py
More file actions
37 lines (24 loc) · 701 Bytes
/
code.py
File metadata and controls
37 lines (24 loc) · 701 Bytes
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
from typing import List
class Student:
def __init__(self, name: str, grades: List[int] = None):
self.name = name
self.grades = grades or []
def take_exam(self, result):
self.grades.append(result)
bob = Student("Bob")
bob.take_exam(90)
print(bob)
# -- as dataclass --
from dataclasses import dataclass, field
@dataclass
class Student:
name: str
grades: List[int] = field(
default_factory=list
) # if we want to run a function, use default_factory and it will run the function to generate the default
def take_exam(self, result):
self.grades.append(result)
bob = Student("Bob")
bob.take_exam(90)
print(bob.grades)
print(bob)