1e. Class methods and members Flashcards

1
Q

What are class methods and properties?

A

They are methods and properties that are shared across all instances of a class.

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

What convention is used for naming class attributes?

A

SCREAMING_SNAKE_CASE

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

How do you define a class method?

A

By using the @classmethod decorator. The rest is identical to how you define an instance method.

eg.
@classmethod
def do_something(cls):
    pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the first parameter that a class method takes?

A

The class itself. Like self for instance methods, we don’t need to pass this parameter in manually.

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

What are static methods?

A

They are methods that don’t modify the state of either the class or specific objects.

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

How do you define a static method?

A

By using the @staticmethod decorator. The rest is identical to how you define an instance method.

eg.
@staticmethod
def do_something():
    pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What scenario is a static method most useful for?

A

They are useful when a method doesn’t need to access any properties of an object or a class but still makes sense for it to be a part of a class. In other words, namespacing.

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

How do you define a class attribute?

A

By defining an attribute outside the \_\_init\_\_ function without using self.

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