use*_*623 6 python callback collision blender
我想在列表中添加一个回调函数,这将导致在适当的时候使用一个参数调用该回调。但是,我也希望将回调传递给另一个变量。
注意:我已经习惯std::bind或使用boost::bind过c ++,所以我一直在寻找类似的东西。
注意:顺便说一下,这是在Python中。
冲突问题的示例:
def collision_callback(hit, hitter)
# doing something relevant...
object = create_object()
collision_callbacks.append(collision_callback(_1, object)) # _1 is a placeholder from C++ lol.
# as you can see here _1 is expected
# to be filled in with the hit object.
Run Code Online (Sandbox Code Playgroud)
您可以使用lambda:
>>> def minus(a, b):
... return a - b
...
>>> minus1 = lambda x: minus(x, 1)
>>> minus1(3)
2
Run Code Online (Sandbox Code Playgroud)
另外,您也可以使用functools.partial:
>>> minus1 = functools.partial(minus, b=1)
>>> minus1(4)
3
Run Code Online (Sandbox Code Playgroud)
但是某些内置函数不接受关键字参数。然后回落到lambda。
>>> print(operator.sub.__doc__)
sub(a, b) -- Same as a - b.
>>> minus1 = functools.partial(operator.sub, b=1)
>>> minus1(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sub() takes no keyword arguments
>>> minus1 = lambda x: operator.sub(x, 1)
>>> minus1(9)
8
Run Code Online (Sandbox Code Playgroud)
如果您预先填充前导参数(填充第一个参数中的值),则无所谓:
>>> minus_from_10 = functools.partial(operator.sub, 10)
>>> minus_from_10(7)
3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1408 次 |
| 最近记录: |