Reputation: 8985
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?
Upvotes: 0
Views: 827
Reputation: 15233
If you have data in Dictionary you can do this way
course_summary_struct = namedtuple(
'CourseSummary',
['id', 'display_name', 'location', 'display_coursenum', 'display_organization'])
# Your data
data = {
'id': 1,
'display_name': 'naren',
'location': 'Bengaluru',
'display_coursenum': 11,
'display_organization': ':D'}
course_summary_struct(**data)
# Creates > CourseSummary(id=1, display_name='naren', location='Bengaluru', display_coursenum=11, display_organization=':D')
Or if you have other way use setattr
setattr(course_summary_struct, 'id', 11)
Upvotes: 0
Reputation: 531065
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])
Upvotes: 1
Reputation: 117681
Tuples are immutable and can not be changed after their creation.
You can use recordclass.recordclass
.
Upvotes: 1