I never saw the decorator. Today I flipped through the cookbook and read it for a while.
20.1 obtain new default values in function calls
Task: After the def statement of the function is executed, Python calculates the default value for the optional parameter of the function, but only once.
For some functions, each time you want to call a function, the default value is calculated.
CodeAs follows:
Import copydef freshdefaults (F): "An F-encapsulated decorator that keeps its default value new during calls" fdefaults = f. func_defaults def refresher (* ARGs, ** kwds): f. func_defaults = copy. deepcopy (fdefaults) return F (* ARGs, ** kwds) return refresher # @ freshdefaultsdef packitem (item, PKG = []): PKG. append (item) return pkg a = packitem (1) B = packitem (2) print ID (a), a print ID (B ), B ''' result of using the decorator 47262256 [1] 47263336 [2] ''' result of not using the decorator 45811952 [1, 2] 45811952 [1, 2] '''
At first, I really didn't understand what it meant. Later, after debugging, I understood some details. The result is obvious, that is, the scope of the default value, or the problems of deep copy and shallow copy.
Is the debugging information when B is about to get the value