A class is a data structure that contains attributes and methods.
You have already used classes in Python. For example,
list are both classes.
print(int) # <class 'int'> print(list) # <class 'list'>
list class the attribute is the data stored in the list and
the methods are functions that can be called on the list,
A class instance is an object that is created from a class. Using
int as an example,
5 would be instances of the
We can use the built-in function
isinstance to check if an object is an instance of a class.
Similarly, we can use the built-in function
type to get the class of an object.
isinstance(5, int) # True isinstance(5, list) # False print(type(5)) # <class 'int'>
Creating a class
class is used to create a class.
class ClassDefinition: attribute = 10 def method(): return 20 print(ClassDefinition.attribute) # 10 print(ClassDefinition.method()) # 20
Creating an instance
A class instance is an object that is created from a class.
self argument is automatically passed into instance methods
when the method is called. It can be used
to access the attributes and methods of the object.
class ClassDefinition: attribute = 10 def method(self): return self.attribute * 2 # use the class to create an object class_instance = ClassDefinition() print(class_instance.attribute) # 10 print(class_instance.method()) # 20
Dunder methods are special methods that are used to define the behavior of a class.
__init__ dunder method is one of these special methods. It is called when an object
is created from a class to initialize the object with any attributes that are required.
class ClassDefinition: def __init__(self, attribute): self.attribute = attribute def method(self): return self.attribute * 2 # use the class to create an object class_instance = ClassDefinition(20) print(class_instance.attribute) # 20 print(class_instance.method()) # 40
- Create a class called
- Add two attributes:
agethat can be set when the object is created.
- Create a method called
introducethat prints a string in the format:
Hi, My name is Joe and I am 12 years old.