I am trying to 'destructure' a dictionary and associate values with variables names after its keys. Something like
params = {'a':1,'b':2}
a,b = params.values()
Run Code Online (Sandbox Code Playgroud)
But since dictionaries are not ordered, there is no guarantee that params.values() will return values in the order of (a, b). Is there a nice way to do this?
我想调用带有字段名称过滤器的查询,在运行时我不知道...不确定如何构造变量名称......或者我可能已经累了.
field_name = funct()
locations = Locations.objects.filter(field_name__lte=arg1)
Run Code Online (Sandbox Code Playgroud)
如果funct()返回name将等于
locations = Locations.objects.filter(name__lte=arg1)
Run Code Online (Sandbox Code Playgroud)
不知道该怎么做......
在Python中可能具有以下内容:
>>> vars = {'a': 5}
>>> makevars(vars)
>>> print a
5
Run Code Online (Sandbox Code Playgroud)
因此,makevars将字典转换为变量.(一般来说这叫什么?)