Is it required to name the second argument of the __deepcopy__ function memo, in case __deepcopy__ is called with memo as a keyword argument?
If I simply want to exclude instances from deep copies, would:
obj.__deepcopy__ = lambda self, _: self
be fine, or do I need to pay attention to naming the second argument of the lambda function memo in order to not cause any issues?
My initial thoughts are that the parameter's name is part of the function signature, and memo is not documented to be a positional argument, so it should probably be safer to name the second argument memo, and just add a linter exception for marking the argument unused.
If you want to work within the object, you can do the deep copy as the following code:
With this you can create a copy with the custom modification
copy.deepcopy(memo_obj).The
deepcopyfunction simply invokes a method within the class called__deepcopy__, passing one parameter. You are free to choose any name for that parameter that you prefer.The issue with the
lambdais that it is a lambda function and not a method inside the class. Consequently, you cannot pass theselfparameter to copy the object.Using
lambda self, memo: selfwould be incorrect because thedeepcopyfunction is called with the following code:In this case, memo takes on the value of self, and the lambda expression has two parameters. This configuration will be outside the scope of the copier function, which expects only one parameter.