0

I have the following nested dict:

world = {'europe' :
                     {'france' : ['paris', 'lion'],
                      'uk' : ['london', 'manchester']}},
         {'asia' :
                     {'china' : ['beijing'],
                     {'japan' : ['tokyo']}}

I'm trying the following objects out of it:

class world:
    continents = {} # dict of continents

class continent(world):
    name = '' # name of the continent
    countries = {} # dict of countries

class country(continent):
    name = '' # name of the country
    cities = [] # list of cities

class city(country):
    name = '' # name of the city

The goal is to get all countries from the continent object and alternatively to get the country and the continent names from a city object.

What is the best way to do so in Python?

1
  • 2
    That's not what inheritance is for. Your code reads "a country is a continent" Commented Aug 13, 2015 at 8:08

2 Answers 2

2

Inheriting from "higher" classes is incorrect here. If you inherit a class, that inheriting class is the parent class plus more. You're saying here that country is a continent and also is a world. That is clearly not true and unnecessary.

There is no necessary hierarchical relationship between those four classes. Worlds are worlds, continents are continents, countries are countries and cities are cities. It's enough for continents to contain a list of the countries they hold, or conversely for a country to hold a reference to the continent it's in. The classes themselves do not need a hierarchical relationship.

Consider also whether such a strict 1:1 relationship is useful. There are countries which exist on more than one continent, depending on how exactly you want to define these terms (colonies are fun). How to design this data structure really depends on the concrete goal you have for it.

Sign up to request clarification or add additional context in comments.

Comments

1

Syntactically, the classes should be defined as

class World(object):
    def __init__(self, continents):
        self.continents = continents

class Continent(World):
    def __init__(self, name):
        self.name = '' # name of the continent
    ...

class Country(Continent):
    def __init__(self, name):
        self.name = '' # name of the country
    ...

class City(Country):
    def __init__(self, name):
        self.name = '' # name of the city
    ...

However, in this case it does not make any sense.

Subclassing means something else:

Class Animal(object):
    pass

Class Dog(Animal):
    pass

Class Snake(Animal):
    pass

A dog is a specific type of animal. A dog is an animal. A snake is also an animal.

In your case, a Continent is not a type of World, a Country is not a type of Continent and so on.

Instead you want to relate those classes, which can live as separate classes or they can go one inside the other.

For example

class City(object):
    def __init__(self, name):
        self.name = '' # name of the city

class Country(object, cities):
    def __init__(self, name, cities):
        self.name = name # name of the country
        self.cities = cities # it's a list of Cities instances

class Continent(object):
    def __init__(self, name, countries):
        self.name = name # name of the continent
        self.countries = countries # it's a list of Countries instances

class World(object):
    def __init__(self, continents):
        self.continents = continents # it's a list of Continent instances

france = Country('France', [City('Paris'), City('Annecy'), City('St. Tropez')])
italy = Country('Italy', [City('Rome'), City('Milan')])
uk = Country('UK', [City('London'), City('Bath')])

europe = Continent('europe', [france, italy, uk])
...

Note: the above is just an example. It may not be the best way to do it in python for a number of reasons, depending on how you intend to manipulate the objects.

It's a wide and long subject.

I suggest to look online for a good tutorial about Object Orientation (also called OOP for Object Oriented Programming or OOD for Object Oriented Design).

Here is one tutorial, but there are thousands available online.

After that, you will be able to design the interfaces your objects should expose in order to offer a certain functionality at the local/application level.

Tip: using a RDBM (Relational Data Base Management System), would help you relating and managing the models. Learn about ERD's (Entity-Relationship Diagrams) to help you design your data model.

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.