-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-student.py
More file actions
executable file
·35 lines (30 loc) · 956 Bytes
/
Copy path11-student.py
File metadata and controls
executable file
·35 lines (30 loc) · 956 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
#!/usr/bin/python3
""" 9. Student to JSON """
class Student:
""" defines a student """
def __init__(self, first_name, last_name, age):
"""
initialization of the object
Args:
first_name (str): first name.
last_name (str): last name.
age (int): age.
"""
self.first_name = first_name
self.last_name = last_name
self.age = age
def to_json(self, attrs=None):
if attrs is not None and type(attrs) == list:
filtered_dict = {
key: value for key, value in self.__dict__.items()
if key in attrs
}
return filtered_dict
else:
return self.__dict__
def reload_from_json(self, json):
"""
replaces all attributes of the Student instance
"""
for key, value in json.items():
setattr(self, key, value)