What are class methods, static methods, and instance methods? (AI) Flashcards

(3 cards)

1
Q

What are instance methods?

A
  • Work with both the class and individual instances
  • Accept the self keyword
  • Used for operations on attributes, like getters and setters
  • Example: def double_value(self): self.value *= 2
  • Call Type: my_instance.some_method()

Instance methods are essential for manipulating instance-specific data.

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

What are class methods?

A
  • Associated with the entire class
  • Use the cls keyword instead of self
  • Employed for operations pertaining to the class as a whole
  • Example: @classmethod def initialize(cls, value): cls.class_variable = value
  • Call Type: MyClass.some_method()

Class methods can also be called through an instance, but this is not recommended.

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

What are static methods?

A
  • Defined within the scope of the class
  • Do not require access to instance or class attributes
  • Used as helper functions or for independent operations
  • Example: @staticmethod def is_valid_email(email): return True
  • Call Type: MyClass.some_method()

Static methods are versatile and can be called through both the class and instances.

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