-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path10-student.py
45 lines (37 loc) · 1.24 KB
/
10-student.py
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
43
44
45
#!/usr/bin/python3
"""Module defining the class Student based on 9-student.py"""
class Student:
"""
Class that defines properties of student.
Attributes:
first_name (str): first name of student.
last_name (int): last name of student.
age (int): age of student.
"""
def __init__(self, first_name, last_name, age):
"""Creates new instances of Student.
Args:
first_name (str): first name of student.
last_name (int): last name of student.
age (int): age of student.
"""
self.first_name = first_name
self.last_name = last_name
self.age = age
def to_json(self, attrs=None):
"""Retrieves a dictionary representation of a Student instance.
If attrs is a list of strings, only attribute names contained in,
this list must be retrieved.
Otherwise, all attributes must be retrieved.
Returns:
dict: dictionary representation.
"""
if attrs is None:
return self.__dict__
new_dict = {}
for item in attrs:
try:
new_dict[item] = self.__dict__[item]
except Exception:
pass
return new_dict