@@ -459,12 +459,29 @@ implementation of this behaviour::
459459Classes can alter the default behaviour by providing one or several special
460460methods:
461461
462+ .. method :: object.__getnewargs_ex__()
463+
464+ In protocols 4 and newer, classes that implements the
465+ :meth: `__getnewargs_ex__ ` method can dictate the values passed to the
466+ :meth: `__new__ ` method upon unpickling. The method must return a pair
467+ ``(args, kwargs) `` where *args * is a tuple of positional arguments
468+ and *kwargs * a dictionary of named arguments for constructing the
469+ object. Those will be passed to the :meth: `__new__ ` method upon
470+ unpickling.
471+
472+ You should implement this method if the :meth: `__new__ ` method of your
473+ class requires keyword-only arguments. Otherwise, it is recommended for
474+ compatibility to implement :meth: `__getnewargs__ `.
475+
476+
462477.. method :: object.__getnewargs__()
463478
464- In protocol 2 and newer, classes that implements the :meth: `__getnewargs__ `
465- method can dictate the values passed to the :meth: `__new__ ` method upon
466- unpickling. This is often needed for classes whose :meth: `__new__ ` method
467- requires arguments.
479+ This method serve a similar purpose as :meth: `__getnewargs_ex__ ` but
480+ for protocols 2 and newer. It must return a tuple of arguments `args `
481+ which will be passed to the :meth: `__new__ ` method upon unpickling.
482+
483+ In protocols 4 and newer, :meth: `__getnewargs__ ` will not be called if
484+ :meth: `__getnewargs_ex__ ` is defined.
468485
469486
470487.. method :: object.__getstate__()
@@ -496,10 +513,10 @@ the methods :meth:`__getstate__` and :meth:`__setstate__`.
496513
497514 At unpickling time, some methods like :meth: `__getattr__ `,
498515 :meth: `__getattribute__ `, or :meth: `__setattr__ ` may be called upon the
499- instance. In case those methods rely on some internal invariant being true,
500- the type should implement :meth: `__getnewargs__ ` to establish such an
501- invariant; otherwise, neither :meth: `__new__ ` nor :meth: ` __init__ ` will be
502- called.
516+ instance. In case those methods rely on some internal invariant being
517+ true, the type should implement :meth: `__getnewargs__ ` or
518+ :meth: `__getnewargs_ex__ ` to establish such an invariant; otherwise,
519+ neither :meth: ` __new__ ` nor :meth: ` __init__ ` will be called.
503520
504521.. index :: pair: copy; protocol
505522
@@ -511,7 +528,7 @@ objects. [#]_
511528
512529Although powerful, implementing :meth: `__reduce__ ` directly in your classes is
513530error prone. For this reason, class designers should use the high-level
514- interface (i.e., :meth: `__getnewargs__ `, :meth: `__getstate__ ` and
531+ interface (i.e., :meth: `__getnewargs_ex__ `, :meth: `__getstate__ ` and
515532:meth: `__setstate__ `) whenever possible. We will show, however, cases where
516533using :meth: `__reduce__ ` is the only option or leads to more efficient pickling
517534or both.
0 commit comments