I was wond开发者_运维问答ering if anybody had any ideas on how to easily alias a method (without creating another method) but pass a static argument too? An example (from how we would normally alias the object - but it obviously doesn't work) to demonstrate what I mean.
# Short and to the point
# Normal: alias = method
alias = method("static", arguments)
from functools import partial
alias = partial(method, 'static')
or, slower but without imports:
alias = lambda *args, **kwargs: method('static', *args, **kwargs)
partial is for exactly this purpose; the lambda
method is a little more flexible if you need to interleave predefined and changing arguments.
精彩评论