1An object belonging to a class. e.g. if you had an Employee class, each
2individual employee would be an instance of the Employee class
1class Car: wheels = 4 # <- Class variable def __init__(self, name): self.name = name # <- Instance variable
1class Person:
2 def __init__(self, name, age):
3 self.name = name
4 self.age = age
5
6p1 = Person("John", 36)
7
8p1.age = 40
9
10print(p1.age)
11---------------------------------------------------------------
1240
1
2class Dog(object):
3 def __init__(self, name, age):
4 self.name = name
5 self.age = age
6
7 def speak(self):
8 print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')
9
10JUB0T = Dog('JUB0T', 55)
11Friend = Dog('Doge', 10)
12JUB0T.speak()
13Friend.speak()
1# Instance Method Example in Python
2class Student:
3
4 def __init__(self, a, b):
5 self.a = a
6 self.b = b
7
8 def avg(self):
9 return (self.a + self.b) / 2
10
11s1 = Student(10, 20)
12print( s1.avg() )
1class Car:
2 wheels = 4 # <- Class variable
3
4 def __init__(self, name):
5 self.name = name # <- Instance variable