Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get classes to inherit the attributes of their parent classes? SOLVED

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

UPDATED CODE:

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

class State():
    def __init__(self, state_capital: str, governor: str, country: Country):
        self.state_capital = state_capital
        self.governor = governor
        self.country = country
        self.president = country.president
        self.country_capital = country.country_capital

class County(State):
    def __init__(self, county_seat, state: State):
        self.county_seat = county_seat
        self.state = state
        self.governor = state.governor
        self.state_capital = state.state_capital
        self.president = state.president
        self.country_capital = state.country_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)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading