Clayton Clayton - 2 months ago 23
Python Question

Creating namedtuple instance from already set values

I have a

namedtuple
like this

course_summary_struct = namedtuple(
'CourseSummary',
['id', 'display_name', 'location', 'display_coursenum', 'display_organization']
)


I want to update the the
namedtuple
dynamically until I am sure it has all required information.

So i Update
course_summary_struct
with all the values in steps.

Now I am at a place where I need to create an instance from
course_summary_struct
like this

summery = course_summary_struct() #<===== Issue here


I want to fill
summery
with all the information in
course_summary_struct
.

How can I do that?

Answer Source

Wait until you have all the necessary information before actually creating the named tuple instance.

data = {}
data['id'] = 3
# ...
data['display_name'] = 'Chem 101'
# ...
summary = course_summary_struct._make([data[x] for x in course_summary_struct._fields])