TypeError: object.__init__() takes no parameters

I’m having trouble a TypeError.

Whenever I create a new HighSchool object I get the error: “TypeError: object.init() takes no parameters”. The HighSchool constructor is built exactly like the PrimarySchool constructor, which performs just fine. The difference in the constructor is in the pickupPolicy vs sportsTeams parameter passed through the various constructors.

I imagine pickupPolicy vs sportsTeam parameters are what’s causing the error… but I just…can’t…figure… it… out!

Link to project: https://www.codecademy.com/courses/learn-intermediate-python-3/projects/python-school-catalogue

Please help :slight_smile:

class School:

  def __init__(self, name, level, numberOfStudents):

    self.name = name

    self.level = level

    self.numberOfStudents = numberOfStudents

  

  def get_name(self):

    return self.name

  def get_level(self):

    return self.level

  def get_numberOfStudents(self):

    return self.numberOfStudents

  

  def set_numberOfStudents(self, numberOfStudents):

    self.numberOfStudents = numberOfStudents

  def __repr__(self):

    return "A {level} school named {name} with {numberOfStudents} students".format(level = self.level, name = self.name, numberOfStudents = self.numberOfStudents)

class PrimarySchool(School):

  

  def __init__(self, name, numberOfStudents, pickupPolicy):

    super().__init__(name, "primary", numberOfStudents)

    self.pickupPolicy = pickupPolicy

  

  def getPickupPolicy(self):

    return self.pickupPolicy

  def __repr__(self):

    print(super().__repr__())

    return "{name} has a pickup policy of {pickupPolicy}".format(name = self.name, pickupPolicy = self.pickupPolicy)

class HighSchool:

  

  def __init__(self, name, numberOfStudents, sportsTeams):

    super().__init__(name, "high", numberOfStudents)

    self.sportsTeams = sportsTeams

  def getSportsTeams(self):

    return self.sportsTeams

  def __repr__(self):

    print(super().__repr__())

    return "The sports teams at {name} are {sportsTeams}".format(name = self.name, sportsTeams = self.sportsTeams)

highSchool = HighSchool("Scots", "1200", ['Basketball', 'Skiing'])

That is a suspicious bit of code (in two places).

If you are wanting to inherit from a parent class, something is missing. :wink:

omg. right there in front of me haha. Thank you.

1 Like

I agree!
The suggestion in the task mentions… “You can call super().__repr__() to avoid repeating some code.
I can’t figure out how to reuse super().repr() while still overloading it with the child class specific output.

eg. I want to use School’s repr() + HighSchool’s repr()

Not convinced it can be done. Need some time to look into it.


On a side note, I’m not sure we should have any dunder method calls in our code. They are accessible through other means.

print (instance)

will invoke the __repr__() method which should return something we can print. We don’t need to vacate our normal paradigm.

From what I understand what you’ve done is not ‘wrong’, in that it should work fine for the exercise. It may cause unexpected issues in different circumstances, though.
What the exercise is suggesting though is adding the ‘super’ call to the string you’re returning.
e.g

superString = super().__repr__()
return superString + "\nClass specific bit of text"

That way you’re returning the string in its entirety.

Thanks.

What’s the risk of having dunder calls in our code?

thanks!

That makes sense. Convert the super() into a string and do something with it. Much appreciated.

1 Like

One cannot say that they are associated with any risk. Dunder methods are meant to be internal, if I’m not mistaken. If you examine the attributes of an object (>>> dir(object)) you will see all the methods available to that object, among which are several dunder methods, and a selection of methods for normal use, along with any methods one may have written on the object.

There will always be exceptions, one supposes, but it is worth reading up on them when you have some free study time.