解开字典的惯用方式是使用双星运算符
**。
配合使用
flask-sqlalchemy:
class Employee(db.Model) id = db.Column(...) firstname = db.Column(...) lastname = db.Column(...)employee_data = {'firstname':'John','lastname':'Smith'}employee = Employee(**employee_data)db.session.add(employee)db.session.commit()请注意,字典中的键必须与类的属性名称匹配。以这种方式打开包装与:
employee = Employee(firstname='John', lastname='Smith')
如果你
__init__使用位置参数定义了一个(或其他方法),但是你也可以使用列表来执行此操作,但是你仅使用单个星号:
def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastnameemployee_data = ['John', 'Smith']employee = Employee(*employee_data)...
注意这里的值顺序很重要。



