5

I'm fairly new to Python and I have a question about a neat way of creating multiple objects with different properties. At the moment I am having to specifically create each one like this:

object1 = MyClass(property=foo,property2=bar)
object1.DoStuff(variable = foobar)

object2 = MyClass(property=foo,property2=bar2)
object2.DoStuff(variable = foobar)

object3 = MyClass(property=foo,property2=bar3)
object3.DoStuff(variable = foobar)

My problem is that I want to create dozens of objects in this way and having to manually create each one like this seems like very bad programming. Is there a better way to create multiple objects with slightly different properties?

I guess what I want to do is something like:

list = [prop1, prop2, prop3, prop4, prop5]
while i < len(list)
    object_i = MyClass(property=foo,property2=list[i])
    object_i.DoStuff(variable = foobar)
    i+=1

And have this create 5 objects named object_1, object_2, etc etc

Is this possible?

3
  • 2
    Have you tried using dictionaries instead? Commented Apr 12, 2012 at 15:25
  • 2
    You should keep data (including indices) out of your variable names. Commented Apr 12, 2012 at 15:27
  • @vartec No, I'm long past the homework stage...just new to programming! Commented Apr 12, 2012 at 15:34

3 Answers 3

12

If you have objects with the names object_1, object_2 etc. and want to perform common operations on all these objects, this is a clear sign that you should actually be using a list objects, and use loops to perform the common operations.

props = <some list here>
objects = [MyClass(property=foo, property2=prop) for prop in props]
for obj in objects:
    obj.do_stuff(variable=foobar)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It seems really obvious when you see it! :-)
5

Another way of going about this could be using more meaningful object definitions with the help of a dictionary. Sven Marnach's solution works well if all you care about is object_1, object_2, etc. but what if your objects actually have individual importance later? Referencing them will become unintuitive and you'll get lost pretty fast. Here's some sample code to help you out in this regard:

objectNames = ("foo", "bar", "cat", "mouse")
objectDictionary = {}
for name in objectNames:
    objectDictionary[name] = MyClass(property=foo,property2=bar)

for obj in objectDictionary.itervalues():
    obj.DoStuff(variable = foobar)

If you want a specific instance to do something different from the others, this solution makes this easy:

objectDictionary["bar"].DoSomethingElse(variable = cat)

Comments

2

Use tuple unpacking along with a list comprehension.

object_1, object_2, ... = [MyClass(property=foo, property2=x) for x in (bar,
  bar2, ...)]

2 Comments

Thanks. That makes sense. How do you then do object_1.DoStuff, object_2.DoStuff, etc?
Sure. But if you're going to do it to every single object then you should put them in a list as Sven says.

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.