On 12/26/11 21:48 , Fredrik Tolf wrote:
On Mon, 26 Dec 2011, K. Richard Pixley wrote:
I don't understand. Can anyone explain?

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.

* Is there any part of the manual that explains, holistically, the
greater context of object instantiation into which __new__ fits? I can
only find small parts of it spread around the documentation for __new__
and __init__, but no complete explanation of it. There are several
things I'm wondering about, like what it means to call a type object at
all; how methods, properties and the like are bound; how pickle can
instantiate a class without calling __init__; when and whether __dict__
is created and a couple of other things. Is there a complete
documentation of the process anywhere that I haven't managed to find?

Everything I know about this stuff I got by reading the manual. Search for __init__ and you'll find a pretty complete description of how objects are created, how to perturb that process, how attributes are looked up, and how to perturb that process. At least, you do in both python 2 and python 3 current manuals, I think.

The conceptual leap for me was in recognizing that a class is just an object. The best way, (imo, so far), to create a singleton in python is to use the class itself as the singleton rather than ever instantiating it. With a little work, you can even prevent it from ever being instantiated.

The concept of a "factory" gets a little weird then, as a factory might return an instantiation of some class but it might instead return a class proper - perhaps even an automatically constructed class - which is a somewhat different sort of factory.

Calling a type is a little bit different. Calling a class is how you initiate an instantiation of that class. Calling an instantiation leads to __call__, (which may or may not have much semantic meaning, depending on your class). It's the difference between C() and C()(). (And the analogy holds for other builtin types, I think).

Super() is also fairly well documented.

But __metaclass__ could use a few more examples, imo. I'm still not entirely clear on __metaclass__. As I understand it, (which may well be completely wrong), __metaclass_ is the means of perturbing the results of "isinstance". You can create, (or delete), inheritance arbitrarily, and without even requiring real, existing classes to do it. I'm not clear on why you'd want to do this, nor why __metaclass__ is a better mechanism than, say, any of the implementations of "interface" which have been done in various tool kits or "mixins", which all seem to be somewhat overlapping in functionality.

--rich
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to