Home > database >  How to get classes to inherit the attributes of their parent classes?
How to get classes to inherit the attributes of their parent classes?

Time:10-29

I am trying to get classes to inherit attributes of the parent class.

The main class is Country and has two attributes that're the country's capital and president.

The class State is derived from Country, and should have the attributes of the state's capital and the governor.

The class County is derived from State, and should have the attribute of the county's name.

I have a pretty basic understanding of classes, any help would be greatly appreciated.

Here is the code I have: Note: Nothing underneath if name = 'main': can change

class Country:
    def __init__(self, country_capital, president):
        self.country_capital = country_capital
        self.president = president

class State(Country):
    def __init__(self, state_capital, governor, c):
        self.state_capital = state_capital
        self.governor = governor
        c = Country()


class County(State):
    def __init__(self, county_seat, c):
        self.county_seat = county_seat
        c = State()
        self.governor = super().__init__(self, state_capital)


if __name__ == '__main__':
    United_States = Country("Washington, DC", "Joe Biden")
    Kentucky = State("Frankfort", "Andy Beshear", United_States)
    Jefferson = County("Louisville", Kentucky)
    print("County seat: ", Jefferson.county_seat)
    print(" Governor: ", Jefferson.governor)
    print("State capital: ", Jefferson.state_capital)
    print("Country capital: ", Jefferson.country_capital)
    print("President:", Jefferson.president)

CodePudding user response:

Since you're constructing the "parent" object separately from the "child", I might suggest composition rather than inheritance. Note that inheritance implies an "is a" relationship rather than an "has a" relationship -- is it correct to say that a State is a Country, or that a State has a Country that it is in?

class Country:
    def __init__(self, capital: str, president: str):
        self.capital = capital
        self.president = president

class State:
    def __init__(self, capital: str, governor: str, country: Country):
        self.capital = capital
        self.governor = governor
        self.country = country

class County:
    def __init__(self, seat: str, state: State):
        self.seat = seat
        self.state = state
        self.country = state.country

if __name__ == '__main__':
    United_States = Country("Washington, DC", "Joe Biden")
    Kentucky = State("Frankfort", "Andy Beshear", United_States)
    Jefferson = County("Louisville", Kentucky)
    print("County seat: ", Jefferson.seat)
    print("Governor: ", Jefferson.state.governor)
    print("State capital: ", Jefferson.state.capital)
    print("Country capital: ", Jefferson.country.capital)
    print("President:", Jefferson.country.president)

CodePudding user response:

You need to initialize the superclass in the constructor like this:

class Country:
    def __init__(self, country_capital, president):
        self.country_capital = country_capital
        self.president = president

class State(Country):
    def __init__(self, state_capital, governor, country_capital, president):
        super().__init__(country_capital, president)
        self.state_capital = state_capital
        self.governor = governor
       
class County(State):
    def __init__(self, county_seat, state_capital, governor, country_capital, president):
        super().__init__(self, state_capital, governor, country_capital, president)
        self.county_seat = county_seat

However, this is not really a natural use of inheritance. You might consider just storing a State as an attribute of a County, rather than its superclass. Same with State and Country.

CodePudding user response:

When using inheritance, we should employ the "is a" mentality. This doesn't quite make sense with your code because a State is not a Country and so it wouldn't have the same attributes.

You should be using attributes to signify that a State has a country that is a part of (like you have)

To answer your question, using super(). __init__() will initialize the same attributes as the parent, provided they are initialized in the parent's constructor.

  • Related