0
0
Pythonprogramming~5 mins

Difference between method types in Python - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is an instance method in Python?
An instance method is a function defined inside a class that operates on an instance of the class. It takes <code>self</code> as the first parameter, which refers to the specific object calling the method.
Click to reveal answer
beginner
What is a class method in Python?
A class method is a method that takes the class itself as the first argument, usually named <code>cls</code>. It is marked with the <code>@classmethod</code> decorator and can access or modify class state shared by all instances.
Click to reveal answer
beginner
What is a static method in Python?
A static method is a method inside a class that does not take <code>self</code> or <code>cls</code> as the first parameter. It behaves like a regular function but belongs to the class's namespace. It is marked with the <code>@staticmethod</code> decorator.
Click to reveal answer
intermediate
How do instance, class, and static methods differ in how they are called?
Instance methods are called on objects and receive the object as <code>self</code>. Class methods are called on the class and receive the class as <code>cls</code>. Static methods can be called on the class or instance but do not receive any automatic first argument.
Click to reveal answer
intermediate
Why use a class method instead of an instance method?
Use a class method when you need to access or modify class-level data that is shared across all instances, or when you want to create alternative constructors. Instance methods are for behavior related to individual objects.
Click to reveal answer
Which method type automatically receives the instance as the first argument?
ANone of the above
BClass method
CStatic method
DInstance method
Which decorator is used to define a class method?
A@staticmethod
B@instance
C@classmethod
D@class
What is true about static methods?
AThey receive the class as the first argument
BThey do not receive any automatic first argument
CThey receive the instance as the first argument
DThey can only be called on instances
When would you use a class method?
ATo access or modify class-level data
BTo access or modify instance-specific data
CTo create a regular function inside a class
DTo override instance methods
Which method type can be called on both the class and its instances?
AStatic method
BClass method
CNone of the above
DInstance method
Explain the differences between instance, class, and static methods in Python.
Think about what each method receives as the first parameter and what data it can access.
You got /3 concepts.
    When should you choose to use a class method over an instance method?
    Consider the difference between data that belongs to the whole class versus data that belongs to one object.
    You got /3 concepts.