开发者

Alias method and pass static argument too

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜