Previous

Classes and Objects

3 / 5
Next

Instead of declaring an object p = Person() and then setting the name p.name = "Maria", we can use class constructors.

class Person:
  def __init__(self, new_name):
    self.name = new_name

p = Person("Elliot")
print(p.name) # output: Elliot

This constructs an object p of class Person. Upon construction, the method __init__, if defined, is called automatically. In this case, the name will be set to Elliot.

Send

The constructor method

Hint
Beginner

Modify the class Point such that points can be constructed with their coordinate values.

Both coordinate parameters should default to 0.

Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!