Skip to content

Commit 5c0be6b

Browse files
Update P03_InstanceAttributes.py
1 parent ed96a4f commit 5c0be6b

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

OOP/P03_InstanceAttributes.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
#Author: OMKAR PATHAK
2-
#In this example we will be seeing how instance Attributes are used
3-
#Instance attributes are accessed by: object.attribute
4-
#Attributes are looked First in the instance and THEN in the class
1+
# Author: OMKAR PATHAK
2+
# Demonstration of Instance Attributes
53

64
import random
7-
class Vehicle():
8-
#Class Methods/ Attributes
5+
6+
class Vehicle:
7+
98
def type(self):
10-
#NOTE: This is not a class attribute as the variable is binded to self. Hence it becomes
11-
#instance attribute
12-
self.randomValue = random.randint(1,10) #Setting the instance attribute
9+
# Creating instance attribute
10+
self.randomValue = random.randint(1, 10)
1311

12+
# Creating object
1413
car = Vehicle()
15-
car.type() #Calling the class Method
16-
print(car.randomValue) #Calling the instance attribute
14+
15+
# Calling method
16+
car.type()
17+
18+
# Accessing instance attribute
19+
print("Random Value:", car.randomValue)

0 commit comments

Comments
 (0)