homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: descrintro describes __new__ and __init__ behavior wrong
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bethard, facundobatista, mcherm
Priority: normal Keywords:

Created on 2005年02月16日 06:48 by bethard, last changed 2022年04月11日 14:56 by admin. This issue is now closed.

Messages (3)
msg60659 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2005年02月16日 06:48
The current descrintro.html says:
"__new__ must return an object... If you return an
existing object, the constructor call will still call
its __init__ method. If you return an object of a
different class, its __init__ method will be called..."
This suggests that __new__ will call __init__ of the
object even if the object is a different type. The
code in typeobject.c disagrees with this:
static PyObject *
type_call(PyTypeObject *type, PyObject *args, PyObject
*kwds)
{
	...
	/* If the returned object is not an instance of type,
	 it won't be initialized. */
	if (!PyType_IsSubtype(obj->ob_type, type))
		return obj;
	...
}
Suggested fix:
"__new__ must return an object... If you return an
existing object, the constructor call will still call
its __init__ method unless the object is an instance of
a different class..."
msg60660 - (view) Author: Michael Chermside (mcherm) (Python triager) Date: 2005年02月23日 15:02
Logged In: YES 
user_id=99874
I agree strongly that the docs need to be clarified.
I spent a while playing around with it and confirmed that
your reading of it appears to be correct, and your proposed
wording is simple and straightforward.
I second the request to make this change.
msg63061 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008年02月27日 02:53
Documentation had several updates in these 2+ years.
Now it says:
"""
If __new__() returns an instance of cls, then the new instance’s
__init__() method will be invoked like __init__(self[, ...]), where self
is the new instance and the remaining arguments are the same as were
passed to __new__().
If __new__() does not return an instance of cls, then the new instance’s
__init__() method will not be invoked.
"""
, which is clearer.
Thanks!
History
Date User Action Args
2022年04月11日 14:56:09adminsetgithub: 41584
2008年02月27日 02:53:05facundobatistasetstatus: open -> closed
resolution: fixed
messages: + msg63061
nosy: + facundobatista
2005年02月16日 06:48:37bediviere.historiccreate

AltStyle によって変換されたページ (->オリジナル) /