Class Attributes

TheΒ 
valueΒ 
ofΒ 
class
Β 
attributes
Β 
remainΒ 
theΒ 
sameΒ 
forΒ 
allΒ 
theΒ 
objectsΒ 
createdΒ 
fromΒ 
theΒ 
class.
TheseΒ 
areΒ 
initialized
Β 
outsideΒ 
theΒ 
classΒ 
Constructor
.
class class_name:
    attribute_1 = value #this is a class attribute
    def __init__(self, attribute_2):
        self.attribute_2 = attribute_2 
        #this is an instance attribute
class Car:
    mfg_year = 2021
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price = price
honda = Car("Honda", "Red", 3500)
print(honda.name, honda.color, honda.price, honda.mfg_year)

Instance Attribute

TheΒ 
valueΒ 
ofΒ 
instance
Β 
attributes
Β 
changeΒ 
forΒ 
differentΒ 
objectsΒ 
createdΒ 
fromΒ 
thatΒ 
class.Β 
TheyΒ 
areΒ 
initializedΒ 
insideΒ 
theΒ 
classΒ 
Constructor
.
WeΒ 
canΒ 
useΒ 
theseΒ 
attributesΒ 
byΒ 
usingΒ 
theΒ 
objectΒ 
name.
object_name.attribute_name

Calling a Method

AnΒ 
objectΒ 
canΒ 
call
Β 
theΒ 
methodsΒ 
insideΒ 
aΒ 
class.
WhenΒ 
aΒ 
methodΒ 
isΒ 
called,Β 
theΒ 
objectΒ 
nameΒ 
isΒ 
sentΒ 
toΒ 
"
self
"Β 
andΒ 
henceΒ 
theΒ 
instance
Β 
attributes
Β 
canΒ 
beΒ 
accessed.
object_name.method_name(arguments)
class Car:
    mfg_year = 2021
    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price = price
    def drive(self, speed):
         print("Driving", self.name, "at", speed, "kmph")
honda = Car("Honda", "Red", 3500)
honda.drive(50)