3

I'm making a program for a school project. It's a Model United Nations.

countries = ['Australia', 'Brazil', 'Canada']

class delegate(object):
   def __init__(self, country):
     self.country = country

I have a list with countries and a class called delegate. So, each delegate object must have a country. Normally I would do it like this:

mexico = delegate('Mexico')

And that would be it. But I would like to loop over the country list and create class instances for each. I mean:

australia = delegate('Australia')
brazil = delegate('Brazil')
canada = delegate('Canada')

and so on. How can I make this? Thank you very much!!

idjaw
  • 25,487
  • 7
  • 64
  • 83
Arturo.Mart
  • 63
  • 1
  • 6
  • I would suggest you re-assess your selection for the answer that helped you. Or at least explain the reasoning why that answer helped you achieve what you are looking for? Currently [this](https://stackoverflow.com/a/44981035/1832539) answer hits exactly what you are looking for. – idjaw Jul 08 '17 at 00:18
  • @idjaw Hi, I'm new on Python (also programming) and I tried the answer you indicated and it shows an error, the one I chose did not. I didn't know why, but if you can explain me I'll be glad in changing my chose. Actually, I had. This the error it shows: delegates = {country: Delegate(country) for country in countries} ^ SyntaxError: invalid syntax – Arturo.Mart Jul 08 '17 at 01:45
  • I just copy pasted the code and I did not receive a syntax error. I suggest you copy the code and paste it in to a new python file with nothing else and try again to make sure. – idjaw Jul 08 '17 at 01:47
  • I have tried that. Maybe the version? I'm using python 2.6 – Arturo.Mart Jul 08 '17 at 01:48
  • Specifying the version would have helped people answering the question to provide an answer more relevant to your environment. I currently don't have a 2.6 installed on my end. But that would explain it. – idjaw Jul 08 '17 at 01:49

3 Answers3

11

Creating named variables from a list is usually a bad idea. Maybe you could try

countries = ['Australia', 'Brazil', 'Canada']

class Delegate(object):    # according to PEP8, class names should be title-case
    def __init__(self, country):
        self.country = country

# create a dict
delegates = {country: Delegate(country) for country in countries}

Edit: as per @SethMMorton, Python 2.6 does not understand dictionary comprehensions (ie what I used above). You can get the same result using

delegates = dict([(country, Delegate(country)) for country in countries])
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • 3
    nit: py3 note for not *needing* to inherit from object explicitly - > `class Delegate:` should suffice. – idjaw Jul 07 '17 at 23:39
  • I don't understand the dict and when I write it into the program it shows a syntax error. delegates = {country: Delegate(country) for country in countries} ^ SyntaxError: invalid syntax – Arturo.Mart Jul 08 '17 at 00:48
  • @Arturo.Mart what version of Python are you using? This works perfectly for me in Python 3.5.3. – Hugh Bothwell Jul 08 '17 at 14:07
  • 1
    @HughBothwell They are using Python 2.6 (as mentioned in the comments of the question). For that version there are no `dict` comprehensions so you might want to mention that `dict([(country, Delegate(country)) for country in countries])` will work for < Python 2.7. – SethMMorton Jul 08 '17 at 19:22
  • @HughBothwell I'm using Python 2.6 So if anyone can help me with this I would be very grateful. – Arturo.Mart Jul 08 '17 at 23:55
1
[delegate(c) for c in countries]
dmitry
  • 4,989
  • 5
  • 48
  • 72
saarrrr
  • 2,754
  • 1
  • 16
  • 26
-3

I believe you're looking for what's called an anonymous class. This means you're making a class without assigning it to a variable. For example:

country_delegates = []
for country in countries:
    country_delegates.append(delegate(country))

This creates a list of delegate objects, one for each country. If you want to get the delegate belonging to a specific country, you could write a function that searches that list and returns the delegate object for the correct country.

KAL
  • 50
  • 2
  • 1
    You aren't making an anonymous class here. You aren't making any class *at all*. I suppose you could torture the `type` constructor to create an "anonymous class", but even then, not really... – juanpa.arrivillaga Jul 07 '17 at 23:49
  • It's very unfortunate this got accepted which ends up providing very misleading guidance to future readers. – idjaw Jul 08 '17 at 00:17
  • Could you elaborate? If you have a class called `delegate` this code works fine. I thought an anonymous class was a class instantiated without being assigned to a variable, is that not right? – KAL Jul 09 '17 at 21:19