python - Generate list of objects named recursively -


my goal list of objects named recursively dynamically. code tried. want object_list [object_0, object_1, object_2, object_3, object_4]

object_list = []  class object(object):       size = ""     price = ""  def create_object_list(object_list):        num in range(5):         object_%s = object() %num           object_list.append(object_%s)     return object_list 

this error:

object_%s = object() %num syntaxerror: can't assign operator 

you trying dynamically create variable names not possible. % operator works strings not identifiers. want use list here of strings.

if want create new variables dynamically can use locals() built-in function contains dictionary of local variables. can example use locals()['object_%d' % num] = object(). globals() function can used if want create global variables.

notice if this, won't list of variable names when print list; you'll list of values.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -