list - Creating a 'grid' of instanecs of a class in Python -


i've been using site lot finding answers various programming questions, , firstly want thank tons of you've been throughout journey of programming. anyway, off question:

i want create list of lists of instances of class. wow, mouthful. let me try explain better in code. example, following 3x3 'grid':

foo = [bar(), bar(), bar()],        bar(), bar(), bar()],        bar(), bar(), bar()]] 

which works perfectly, however, don't know dimensions of 'grid' beforehand. in mind, logical attempt following:

foo = [[bar()]*num]*num 

however, not work properly. believe error python populating 'grid' full of same instance of bar(), problematic. there way 'nice' can complete task of filling grid different instances of bar()?

foo = [[bar() _ in xrange(num)] _ in xrange(num)] 

sequence multiplication makes big grid of references same object. need use list comprehensions evaluate bar() expression repeatedly.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -