循环遍历参数,设置为对象属性

时间:2016-12-26 11:38:59

标签: python python-3.x

我已经定义了一个使用构造函数的类,该构造函数意味着允许任意数量的属性集合:

CultureInfo nonInvariantCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = nonInvariantCulture;

当我尝试创建<portlet:defineObjects /> <script type="script/javascript" src="${renderRequest.contextPath}/js/test.js"/> 时,

class CalendarEvent:
    """Represents a macOS calendar event."""
    def __init__(self, **keyword_arguments):
        self.__allowed_properties = ["starts", "ends", "repeat", "end_repeat", "travel_time", "alert", "notes", "invitees"]
        for key, value in keyword_arguments:
            if key in self.__allowed_properties:
                setattr(self, key, value)

我收到错误:

CalendarEvent

1 个答案:

答案 0 :(得分:2)

直接遍历字典只会迭代其键。你想要:

    for key, value in keyword_arguments.items():