[Python-Dev] PEP 362: 4th edition

Yury Selivanov yselivanov.ml at gmail.com
Tue Jun 19 04:00:57 CEST 2012


On 2012-06-18, at 9:36 PM, Nick Coghlan wrote:

> On Tue, Jun 19, 2012 at 4:09 AM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
>> On 2012-06-18, at 1:35 PM, PJ Eby wrote:
>>> Then just copy the signature itself; as currently written, this is going to copy the annotation objects, which could produce weird side-effects from introspection.  Using deepcopy seems like overkill when all that's needed is a new Signature instance with a fresh OrderedDict.
>> 
>> That's an excerpt from Signature.__deepcopy__:
>> 
>>     cls = type(self)
>>     sig = cls.__new__(cls)
>>     sig.parameters = OrderedDict((name, param.__copy__()) \
>>                           for name, param in self.parameters.items())
>> 
>> And Parameter.__copy__:
>> 
>>        cls = type(self)
>>        copy = cls.__new__(cls)
>>        copy.__dict__.update(self.__dict__)
>>        return copy
>> 
>> So we don't recursively deepcopy parameters in Signature.__deepcopy__
>> (I hope that we don't violate the deepcopy meaning here)
> 
> In my opinion, It's better to redefine what you mean by a shallow copy
> (making it a bit deeper than just the direct attributes) rather than
> making a so-called deep copy shallower.

Agree. That's the only thing about the implementation that I really didn't 
like - deepcopy that's not exactly deep.

> So keep the current copying semantics for Signature objects (i.e.
> creating new copies of the Parameter objects as well), but call it a
> shallow copy rather than a deep copy. Make it clear in the
> documentation that any defaults and annotations are still shared with
> the underlying callable.

So, 'Signature.__deepcopy__()' -> 'Signature.shallow_copy()'?  Or make
it private - 'Signature._shallow_copy()'?

-
Yury



More information about the Python-Dev mailing list