213 Creating classes

  1. What naming convention is typically used for class names in Python?

Show solution

Class names in Python typically use camel case, starting with an uppercase letter. For example: LightSwitch.

  1. What does the colon at the end of a class statement indicate in Python?

Show solution

The colon indicates that the body of the class definition follows. It is similar to how colons are used after def, if, for and other Python statements.

  1. Define a method in Python and describe its characteristics.

Show solution

A method is a function defined inside a class. It always has at least one parameter, usually named self, which refers to the specific object calling the method.

  1. What is the purpose of the init method in a class?

Show solution

The __init__ method is a special method that runs automatically whenever a new object is created from a class. It is typically used to initialise the object’s attributes.

  1. Is the init method required in every Python class? Why might you include it anyway?

Show solution

The __init__ method is not strictly required, but it is good practice to include it because it provides a clear and consistent place to initialise the attributes of an object when it is created.

Last updated

Was this helpful?