Skip to content

Commit a2fdcaa

Browse files
committed
Add prints and missing code files, apply edits
1 parent 21b38b3 commit a2fdcaa

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

python-dict-attribute/config_v3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ def get_option(self, key):
99
return getattr(self, key, None)
1010

1111
def remove_option(self, key):
12-
if key in self.__dict__:
12+
if hasattr(self, key):
1313
delattr(self, key)
1414
print(f"'{key}' removed!")
1515
else:
1616
print(f"'{key}' does not exist.")
1717

1818
def clear(self):
19-
for key in self.__dict__:
19+
for key in list(self.__dict__.keys()):
2020
delattr(self, key)
2121
print("All options removed!")

python-dict-attribute/person_v1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ def as_dict(self):
2020

2121
def as_tuple(self):
2222
return tuple(self.__dict__.values())
23+
24+
25+
person = Person("John", "Doe", 30)
26+
print(repr(person))
27+
print(person)
28+
print(person.as_dict())
29+
print(person.as_tuple())
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Record:
2+
"""Hold a record of data."""
3+
4+
5+
john = {
6+
"name": "John Doe",
7+
"position": "Python Developer",
8+
"department": "Engineering",
9+
"salary": 80000,
10+
"hire_date": "2020-01-01",
11+
"is_manager": False,
12+
}
13+
14+
15+
def as_dict(self):
16+
return self.__dict__
17+
18+
19+
Record.as_dict = as_dict
20+
print(Record.__dict__)
21+
22+
john_record = Record()
23+
john_record.__dict__.update(john)
24+
print(john_record.name)
25+
print(john_record.department)
26+
print(john_record.as_dict())

python-dict-attribute/salary.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Employee:
2+
def __init__(self, name, department, salary):
3+
self.name = name
4+
self.department = department
5+
self.salary = salary
6+
7+
def give_raise(self, amount):
8+
self.salery = self.salary + amount # Typo here: self.salery
9+
10+
11+
john = Employee("John", "Engineering", 70000)
12+
john.give_raise(5000)
13+
print(john.salary)
14+
print(john.__dict__)

0 commit comments

Comments
 (0)