Class and instance attributes:
“Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a “blueprint” for creating objects.”
A class can have attributes (like a characteristics) attached to it, and methods witch represent functions (like actions).
What’s a class attribute:
It’s an attribute witch has been defined outside __init__,
and can be called using the form <class name>.<attribute name>
class Car:
color = "red"
and to use it:
Car.color = "green"
What’s an instance attribute:
It’s the attribute that are created in the __init_ method
and can be called using the form self.<attribute name> :
class Car:
def __init__(self, age=0):
self.age = age
To return age we have to use:
@property
def age(self):
return self.__age
And to check to value:
@age.setter
def age(self, value):
self.__age = value
What are the differences between class and instance attributes:
Class attribute are like general characteristic where it will be applied to every object (like the number of wheels in a car or how many engine it use) but instance attributes are for characteristics like the age of a car or the color.
What are the advantages and drawbacks of each of them: