1

Let's say I have a class called "Map", and Map is constructed with argument "country". When instantiated Map should create a number of object of class "City", based upon a list of city names for country. But how do I name the "city" objects?

pseudocode:

class Map(object):
    def __init__(self, country):
        for i in range(len(citynames)):
            cityname_i = City(i)

In words: so let's say I have list of citynames: ["Boston", "Chicago", "Denver]. When I then do something like

us_map = Map(america)

I would like Map to create three instances of class City (defined elsewhere), named "Boston", "Chigago" and "Denver".

I am new to this, so maybe I got my glossary OOP glossary mixed up. If so please correct me.

Edit

It seems my example created som confusion. The way I see it, my question is not about dictionaries. I just used that as an example.

I am creating a game, were people can upload any number of scenarios. I don't know how many scenarios they are uploading. But all scenarios basically work the same way, with a number of methods. So when I create ´game = Game(folder)´ my class should create an instance of class Scenario for each uploaded scenario file. Then a third class "Engine" plays through all the scenario's...

But how do I assign names to the instances of "Scenario" automatically?

For the moment I am using ´glob´ to find the scenario files in the relevant folder. So I have those in a list, outside the class.

UPDATE:

It seems that either I have not made my question clear (can I create instances automatically on the fly) or that good people would rather point me in the right direction, than answer my foolish question. Anyway - I think that dictionary is the way to go. So for now I am closing this question (rplnt was the first to answer) and awarding the right answer to the first poster.

3 Answers 3

3

Again, as in my previous answer, I would put the objects in a dictionary. Advantage over creating them in a namespace is that they will be easier to maintain. I.e. if you would create them like you said, you would still need to know about them, probably by remembering their reference stored in list (or perhaps dictionary?). If you would want to rely on the list of names, then your code would be full of try statements.

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

1 Comment

something like: cities = {"Boston":{"mayor":"name","inhabitant":"int"}}? I guss that makes sense... Will rewrite some code and try :-)
1

Basically it sounds like you need to create an instance of some kind of container class and store it in an attribute of your Map class instances (i.e. self.cities).

Ideally the type of this container should be something appropriate to how the collection of cities in a Map will be used and/or manipulated. For example will members be added, deleted, or changed frequently? Will they be iterated over often and do they need to be kept in a certain order or reordered on demand?

Depending on your answers to questions like this, Python offers a number of built-in high performance data types, such as lists, dictionaries, and sets, which might be suitable. It also allows you to create your own container classes when that's necessary.

1 Comment

Basically I am trying to create a pimped up version of learnpythonthehardway.org/book/ex43.html. The people who will be playing the game can upload scenarios to a folder. when the game is instantiated a class for each uploaded scenario should be created. Each scenario object has a method, "do_it" which returns a value. the game "play" method now loops over all the scenarios and gives the return value of scenario1 as a parameter to scenario2 etc. until there are no more scenarios.
0

I would do something like this:

class Map(object):

  CITIES = { "america": ["Boston", "Chicago", "Denver"], "germany": ["Munich" , ...], ...}

  def __init__(self, country):
      self.cities = []
      for city_name in Map.CITIES[country].values():
          self.cities.append(City(city_name))

2 Comments

As outlined by rpInt it might be better if self.cities was a dictionary, then us_map.cities['Boston'] would return the City object.
Sorry - apparently my example was not clear... Will try to edit my question.

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.