1

Imagine you have a class called Human which needs a name to be built. Then you have a City(Human) class which inherits Human class where it needs a population and a list of people which consist of instantiation of that Human class. Then a Country class is introduced where similarly, it requires a list of cities.

I wrote this simple code but i don't know exactly how to use super() and it produces errors related to that wrong type of inheritance. I also want to have access to all Human names when i'm in City class for example.

I would appreciate any comment and help from you guys. Here's the code:

class Human:
    def __init__(self, name, **kwargs):
        self.name = name


class City(Human):
    def __init__(self, people_list, population, **kwargs):
        self.population = population
        self.people = people_list

        super(Human, self).__init__()


class Country(City):
    def __init__(self, city_list, **kwargs):
        self.city_list = city_list
        super(City, self).__init__()


if __name__ == '__main__':
    joe = Human("joe")
    lisa = Human("lisa")
    mona = Human("mona")
    alex = Human("alex")

    my_list01 = [joe, lisa]
    my_list02 = [mona, alex]

    London = City(my_list01, 2)
    Leeds = City(my_list02, 2)

    my_list03 = [London, Leeds]

    UK = Country(my_list03)
shahriar
  • 362
  • 4
  • 17
  • 2
    A city is not a human. At best, it is a *collection* of humans, so `City` would not inherit from `Human`; rather, it would only keep a list (or set, or something) of `Human` instances as an attribute. Likewise, a country is not (necessarily) a city. (Something like Monaco could be an instance of a class `CityCountry` that inherits from both `City` and `Country`.) – chepner Sep 11 '19 at 13:01
  • If a city *were* a human, you would need to pass a name to `super(Human, self).__init__()`, just like you would when calling `Human()` directly. You aren't doing that. – chepner Sep 11 '19 at 13:03
  • 2
    inheritance represents "is-a", members represent "has-a" (two importantly different concepts). Granted, if being human is only meaninful insofar as "it has a name" then maybe you just need a better class name. – Kenny Ostrom Sep 11 '19 at 13:03
  • @KennyOstrom @chepner I actually simplified my problem, so there are some methods for each of these classes that i didn't mention. e.g. class `Human` has a 3d coordinate (position) with methods similar to python's Turtle, like turn left, move and etc.similarly the `City` class can contain `Human` and they can walk in their `City` so a `City` is similar to a map, then comes the `Country` which those `Human` can travel between those `Cities` and there are special methods for each of these classes and finally these `Human`s can interact with each other so their attributes (like position) change – shahriar Sep 11 '19 at 13:38
  • 1
    class Country(City): def __init__(self, city_list, **kwargs): self.city_list = city_list super(City).__init__() –  Sep 11 '19 at 16:20
  • 1
    https://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language – Kenny Ostrom Sep 12 '19 at 20:09

0 Answers0