I have an array of classes and I want to create objects of them. This works:
classArray = [Auto, Wheel]
objectArray = []
for myClass in classArray:
objectArray += [myClass()]
Can I use the map function to accomplish the same?
objectArray = map( ??? , classArray)
My apologies if this is a dumb question. I am fairly new to Python.
You could use a list comprehension instead. Many consider them to be preferred over the
mapfunction.If you insist on using
map, you can doHere, I am creating an anonymous function with
lambdathat simply takes the item, and calls it. If you are unfamiliar withmap, it takes a 1-parameter function as the first argument.