How to pass predefined arguments when storing a function

前端 未结 2 1222
臣服心动
臣服心动 2021-01-23 12:38

Is it possible to store a function with predefined arguments to be called by another function?

For example:

def function(num):
    print num

trigger=fun         


        
2条回答
  •  日久生厌
    2021-01-23 12:55

    You're looking for functools.partial:

    >>> import functools
    >>> def foo(number):
    ...     print number
    ... 
    >>> bar = functools.partial(foo, 1)
    >>> bar()
    1
    

提交回复
热议问题