-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses_and_objects.py
51 lines (35 loc) · 1 KB
/
classes_and_objects.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
46
47
48
49
50
51
# defining a class
class MyClass:
a = 3
obj1 = MyClass()
print(obj1.a)
# defining a class and using init function to initialise the name and id
class Employee:
def __init__(self, name, id):
self.name = name
self.id = id
employee1 = Employee("HariOm", 3922)
# Note: The __init__() function is called automatically every time the class is being used to create a new object.
print(employee1.name)
print(employee1.id)
# object methods
# self parameter is not compulsory. we can use any thing at that place
class Employee:
def __init__(self, name, id):
self.name = name
self.id = id
def greet(self):
print("hello Mr." + self.name)
employee1 = Employee("HariOm", 3922)
employee1.greet()
# an object or its properties using del keyword
class Emp:
def __init__(self, name, id):
self.name = name
self.id = id
def greet(self):
print("hello Mr." + self.name)
del Employee.id
del Emp
employee1 = Emp("HariOm", 3922)
employee1.greet()