Skip to content

Commit fccd97e

Browse files
author
Karandeep Grover
committed
fixed issues with 16
1 parent 75596dd commit fccd97e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Exercise: Class and Objects
2+
3+
1. Create a sample class named Employee with two attributes id and name
4+
5+
```
6+
employee :
7+
id
8+
name
9+
```
10+
object initializes id and name dynamically for every Employee object created.
11+
12+
```
13+
emp = Employee(1, "coder")
14+
```
15+
16+
2. Use del property to first delete id attribute and then the entire object
17+
18+
19+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/16_class_and_objects/16_class_and_objects.py)
20+
21+
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Employee:
2+
3+
def __init__(self, id, name):
4+
self.id = id
5+
self.name = name
6+
7+
def display(self):
8+
print(f"ID: {self.id} \nName: {self.name}")
9+
10+
11+
# Creating a emp instance of Employee class
12+
emp = Employee(1, "coder")
13+
14+
emp.display()
15+
# Deleting the property of object
16+
del emp.id
17+
# Deleting the object itself
18+
try:
19+
print(emp.id)
20+
except NameError:
21+
print("emp.id is not defined")
22+
23+
del emp
24+
try:
25+
emp.display() # it will gives error after deleting emp
26+
except NameError:
27+
print("emp is not defined")

0 commit comments

Comments
 (0)