开发者

Alias method and pass static argument too

开发者 https://www.devze.com 2023-03-26 14:26 出处:网络
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 th

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

精彩评论

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