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!!

5
  • 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 answer hits exactly what you are looking for. Commented Jul 8, 2017 at 0: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 Commented Jul 8, 2017 at 1: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. Commented Jul 8, 2017 at 1:47
  • I have tried that. Maybe the version? I'm using python 2.6 Commented Jul 8, 2017 at 1: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. Commented Jul 8, 2017 at 1:49

3 Answers 3

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])
Sign up to request clarification or add additional context in comments.

5 Comments

nit: py3 note for not needing to inherit from object explicitly - > class Delegate: should suffice.
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 what version of Python are you using? This works perfectly for me in Python 3.5.3.
@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.
@HughBothwell I'm using Python 2.6 So if anyone can help me with this I would be very grateful.
1
[delegate(c) for c in countries]

Comments

-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.

3 Comments

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...
It's very unfortunate this got accepted which ends up providing very misleading guidance to future readers.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.