Search code examples
pythoncollectionsnamedtuple

Why does my object is returning property object rather than the value?


I need to convert my Dict to a Object, and for that, I'm using collections.namedtuple.

items = {'name':'blabla', 'car':'hahah', 'dada': 19, 'another':'options'} 

This a dict for this example that I'm doing.. Now I convert:

from collections import namedtuple
Items = namedtuple('Items',items)

When I want to read name, I should call this way > Items.name but what I receive is <property object at 0x0000000002C277C8>. Why is happen?


Solution

  • You're not using namedtuple correctly. It returns a class, you have to create an instance of the class from your dictionary.

    Items = namedtuple('Items', ['name', 'car', 'data', 'another'])
    i = Items(**items)
    print(i.name)