Reputation: 21
if we are given this code:
from collections import namedtuple
Book = namedtuple('Book', 'title author year price')
favorite = Book('Adventures of Sherlock Holmes',
'Arthur Conan Doyle', 1892, 21.50)
another = Book('Memoirs of Sherlock Holmes',
'Arthur Conan Doyle', 1894, 23.50)
still_another = Book('Return of Sherlock Holmes',
'Arthur Conan Doyle', 1905, 25.00)
how do you get the title of the book in the variable still_another to print?
Upvotes: 0
Views: 43
Reputation: 122376
You can do:
print(still_another.title)
Each value in a namedtuple
can be accessed via its name and in your case the title can be accessed via .title
.
Upvotes: 4