On Tue, 27 Dec 2011, Ian Kelly wrote:
On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf <fred...@dolda2000.com> wrote:
I'm also a bit confused about __new__. I'd very much appreciate it if
someone could explain the following aspects of it:
* The manual (<http://docs.python.org/reference/datamodel.html>) says
that __new__ is "a static method (special-cased so you need not declare
it as such)". What does "special-cased" mean? Apparently, for
instance, in OP's case, Python did not automatically detect that it
should not be bound as a method.
It apparently has to do with the class creation. For the
special-casing to happen, the __new__ method has to be present when
the class is created. If it is, then it automatically gets wrapped in
a staticmethod. In the OP's case, he was adding the __new__ method
after class creation, so the wrapping did not happen automatically.
Hmm, thank you. After trying a couple of things, it appears that the
reason OP's method worked in Python 3 is that __get__ on ordinary
functions in Python 3 simply returns the function itself when called on
type lookup, rather than Python 2's unbound method objects.
http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy
"""
Class Types
Class types, or “new-style classes,” are callable. These objects
normally act as factories for new instances of themselves, but
variations are possible for class types that override __new__(). The
arguments of the call are passed to __new__() and, in the typical
case, to __init__() to initialize the new instance.
"""
AFAIK, that's pretty much it. When a type is called, __new__ is
called to create the new instance, and then __init__ is called to
initialize it (if __new__ returned an instance of the type).
Since that description doesn't include when __dict__ is created, it isn't
complete, however. I guess it's safe to assume that __dict__ is created
inside object.__new__, but that also leaves some things unclear; see
below.
how methods, properties and the like are bound;
When they're accessed, using the descriptor protocol, not as part of
the instantiation process. See:
http://docs.python.org/reference/datamodel.html#invoking-descriptors
Ah, sorry. I had read that, but I had mistakenly thought that the same
instance method object was always returned, which would have implied that
it had to be stored for the instance somewhere, at some point. I see now
that that is not actually the case, however. That clears up a whole lot
for me. Thanks!
when and whether __dict__
is created and a couple of other things.
Under the hood as part of the object creation process, unless the
class uses __slots__.
And also unless the object created is of type `object' or any other
built-in type, which leaves me wondering exactly under what circumstances
__dict__ actually is created. Is it some kind of special case in
object.__new__ for types created with the `class' statement? There also
appear to be other special cases in object.__new__:
object.__new__(dict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object.__new__(dict) is not safe, use dict.__new__()
I guess my question reduces into the following: What does object.__new__
actually do, and what other special cases does it implement?
--
Fredrik Tolf
--
http://mail.python.org/mailman/listinfo/python-list