Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff564d3

Browse files
committed
SF #1156412: document the __new__() static method
(merge from release24-maint branch).
1 parent 50682d0 commit ff564d3

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Doc/ref/ref3.tex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,35 @@ \section{Special method names\label{specialnames}}
10521052

10531053
\subsection{Basic customization\label{customization}}
10541054

1055+
\begin{methoddesc}[object]{__new__}{cls\optional{, \moreargs}}
1056+
Called to create a new instance of class \var{cls}. \method{__new__()}
1057+
is a static method (special-cased so you need not declare it as such)
1058+
that takes the class of which an instance was requested as its first
1059+
argument. The remaining arguments are those passed to the object
1060+
constructor expression (the call to the class). The return value of
1061+
\method{__new__()} should be the new object instance (usually an
1062+
instance of \var{cls}).
1063+
1064+
Typical implementations create a new instance of the class by invoking
1065+
the superclass's \method{__new__()} method using
1066+
\samp{super(\var{currentclass}, \var{cls}).__new__(\var{cls}[, ...])}
1067+
with appropriate arguments and then modifying the newly-created instance
1068+
as necessary before returning it.
1069+
1070+
If \method{__new__()} returns an instance of \var{cls}, then the new
1071+
instance's \method{__init__()} method will be invoked like
1072+
\samp{__init__(\var{self}[, ...])}, where \var{self} is the new instance
1073+
and the remaining arguments are the same as were passed to
1074+
\method{__new__()}.
1075+
1076+
If \method{__new__()} does not return an instance of \var{cls}, then the
1077+
new instance's \method{__init__()} method will not be invoked.
1078+
1079+
\method{__new__()} is intended mainly to allow subclasses of
1080+
immutable types (like int, str, or tuple) to customize instance
1081+
creation.
1082+
\end{methoddesc}
1083+
10551084
\begin{methoddesc}[object]{__init__}{self\optional{, \moreargs}}
10561085
Called\indexii{class}{constructor} when the instance is created. The
10571086
arguments are those passed to the class constructor expression. If a

0 commit comments

Comments
 (0)