OOP Flashcards

1
Q

what is difference between a class method and a static method?

A
1 - A class method takes cls as first parameter while a static method needs no specific parameters.
2 - A class method can access or modify class state while a static method can’t access or modify it.
3  - In general, static methods know nothing about class state. They are utility type methods that take some parameters and work upon those parameters. On the other hand class methods must have class as parameter. 
4 - We use @classmethod decorator in python to create a class method and we use @staticmethod decorator to create a static method in python.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

when we use static method?

A

We generally use static methods to create utility functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

when we use class method?

A

We generally use class method to create factory methods. Factory methods return class object ( similar to a constructor ) for different use cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when we use Metaclasses?

A
1 -metaclasses propogate down the inheritance hierarchies. It will affect all the subclasses as well. If we have such situation, then we should use metaclasses.
2 - If we want to change class automatically, when it is created
3 - If you are API developer, you might use metaclasses
How well did you know this?
1
Not at all
2
3
4
5
Perfectly