In Python 3.x official documents, we could find the source code about functools.partial, below:
def partial(func, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*args, *fargs, **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
However, I can't understand the syntax for below
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
Where could see about these similar syntax? I am pretty puzzled. Because, I got a fatal error when I try the below code:
def func(A, B):
def func_m(*fargs, **fkargs):
print(D+C)
print(fargs, fkargs)
func_m.D = A
func_m.C = B
return func_m
f = func(1, 2)
f()
But i got a NameError that name 'D' is not defined after i call f().
Save my time, please! Thanks.
DandC? Couldn't you useAandBdirectly insidefunc_m?=lines, that’s just an assignment like any other. Notice how you’re introducing previously undefined variablesDandC, while all ofpartials names are previously defined.