-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-student.py
More file actions
executable file
·42 lines (32 loc) · 1.19 KB
/
11-student.py
File metadata and controls
executable file
·42 lines (32 loc) · 1.19 KB
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
38
39
40
41
42
#!/usr/bin/python3
"""Defines a class Student."""
class Student:
"""Represent a student."""
def __init__(self, first_name, last_name, age):
"""Initialize a new Student.
Args:
first_name (str): The first name of the student.
last_name (str): The last name of the student.
age (int): The age of the student.
"""
self.first_name = first_name
self.last_name = last_name
self.age = age
def to_json(self, attrs=None):
"""Get a dictionary representation of the Student.
If attrs is a list of strings, represents only those attributes
included in the list.
Args:
attrs (list): (Optional) The attributes to represent.
"""
if (type(attrs) == list and
all(type(ele) == str for ele in attrs)):
return {k: getattr(self, k) for k in attrs if hasattr(self, k)}
return self.__dict__
def reload_from_json(self, json):
"""Replace all attributes of the Student.
Args:
json (dict): The key/value pairs to replace attributes with.
"""
for k, v in json.items():
setattr(self, k, v)