为什么我的对象返回属性对象而不是值?

问题描述 投票:0回答:1

我需要将Dict转换为对象,为此,我正在使用collections.namedtuple

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

这是我正在执行的此示例的字典。现在,我进行转换:

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

[当我想读名字时,我应该这样称呼>> Items.name,但是我收到的是<property object at 0x0000000002C277C8>。为什么会发生?

python collections namedtuple
1个回答
2
投票
您没有正确使用namedtuple。它返回一个类,您必须从字典中创建该类的实例。

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

© www.soinside.com 2019 - 2024. All rights reserved.