+ 1

How to get a placeholder for lists? I'm trying to create class instances but one of my parameters is a list of lists.

https://code.sololearn.com/cg6sfNkyq2kN/?ref=app

27th Apr 2020, 8:55 AM
Catherine James
Catherine James - avatar
6 Respostas
+ 2
I rewrote your program a little. added also the @property decorators because this is more pythonic than getter methods. https://code.sololearn.com/cR4DGo503Lih/#py
27th Apr 2020, 9:37 AM
Tibor Santa
Tibor Santa - avatar
+ 1
It's a little confusing what you are trying to do there. Using exec() is definitely a bad idea, there must be a better way. Also, looping through a dictionary can be done like: for key, value in dict.items() Can you explain the data structure of your MyClasses dictionary? I assume you want to make instances of Course from that data. Just the values are not too meaningful.
27th Apr 2020, 9:08 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Oh thank youuu so muchh. But is it possible to make the keys as variables and assign each item in myInstances to each of them like myObj01=Course("Foo", "Bar",[["3","k"],["2","l"]], 65) so when i print myObj01.name I'd get "Bar"?
27th Apr 2020, 10:11 AM
Catherine James
Catherine James - avatar
+ 1
Well yes, it would be possible as you have already done with the exec, but it doesn't really make sense. A variable name is a reference to one thing. But if you have to make more than 2 instances, say 5, or 20, or 5000, how do you track all those variables. The solution is to use collections, exactly as you have done with myInstances. So you can refer to each object as myInstances[0] and so on. Or you can also make myInstances a dict instead of a list: use the key that was in the original data, so you can refer them as myInstances['myObj01']
27th Apr 2020, 10:22 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks alot Sir. This was very helpful.
27th Apr 2020, 12:03 PM
Catherine James
Catherine James - avatar
0
I want the keys in MyClasses dict to be the instances and the values to be the parameters sent to the class Course. The exec line worked when the parameters were just strings and integers. But i need the list of lists as well.
27th Apr 2020, 9:13 AM
Catherine James
Catherine James - avatar