Discuss / Python / partial----部分,译“偏”的是高数的渣

partial----部分,译“偏”的是高数的渣

Topic source

ywjco_567

#1 Created at ... [Delete] [Delete and Lock User]

partial 英 [ˈpɑːʃl] 美 [ˈpɑːrʃl]

adj.     部分的; 不完全的; 热爱; 钟爱; 偏颇; 偏袒;

[例句]He managed to reach a partial agreement with both republics.

他设法与两个共和国都达成了部分协议。

别被“*args,**kwargs”绕晕。举个简单点例子:

>>> from functools import partial

>>> def add(a,b,c=2):
        print("a is:%s b is %s c is %s"%(a,b,c))
        return a+b+c

>>> add_a_b=partial(add,2,3)

>>> print(add_a_b())
a is:2 b is 3 c is 2
7

>>> print(add_a_b(5))
a is:2 b is 3 c is 5
10

>>> print(add_a_b(5,10))
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    print(add_a_b(5,10))
TypeError: add() takes from 2 to 3 positional arguments but 4 were given

>>> print(add_a_b(c=10))
a is:2 b is 3 c is 10
15

  • 1

Reply