-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasics.py
More file actions
136 lines (63 loc) · 2.51 KB
/
Copy pathbasics.py
File metadata and controls
136 lines (63 loc) · 2.51 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# creating class variables
class Employee:
fname = "steve"
lname = "jobs"
emp1 = Employee()
emp2 = Employee()
# accessing class variables using objects
# print(emp1.fname)
# print(emp2.lname)
# accessing class variables using classname
# print(Employee.fname)
# checking the attributes of class
# print(Employee.__dict__)
# checking the attributes of objects
# print(emp1.__dict__)
###########################################################################
# creating instance/object variables in class
class Employee:
def __init__(self, fname, lname): # initializer - initializes object variables
self.fname = fname
self.lname = lname
emp1 = Employee("mark", "zuckerberg")
emp2 = Employee("Rangnath", "swamy")
# accessing instance variables using objects
# print(emp1.fname) # mark
# print(emp2.fname) # Rangnath
# accessing instance variables using class
# print(Employee.fname) # AttributeError
# checking the attributes of class
# print(Employee.__dict__) # {__init__: address, other inbuilt attributes of class}
# checking the attributes of instances
# print(emp1.__dict__) # {"fname": "mark", "lname": "zuckerberg"}
# print(emp2.__dict__) # {"fname": "Rangnath", "lname": "swamy"}
###############################################################################################
# creating both class and instance variables with same name
class Employee:
fname = "steve" # class variables
lname = "jobs"
def __init__(self, fname, lname):
self.fname = fname # object/instance variables
self.lname = lname
e1 = Employee("tata", "birla")
e2 = Employee("Mukesh", "Ambani")
# print(e1.fname) # tata
############################################################################################
class Employee:
fname = "steve" # class variables
lname = "jobs"
def __init__(self, age, salary):
self.age = age # object/instance variables
self.salary = salary
emp1 = Employee(20, 30000)
emp2 = Employee(30, 40000)
# modification w.r.t objects
# emp1.fname = "Einstien"
# print(emp1.fname) # Einstien
# print(emp2.fname) # steve
# print(Employee.fname) # steve
# modification w.r.t class
# Employee.fname = "Ratan"
# print(Employee.fname) # Ratan
# print(emp1.fname) # Ratan
# print(emp2.fname) # Ratan