autoborg &c (was )e: Unified Type/class (Singleton)
Pedro Rodriguez
pedro_rodriguez at club-internet.fr
Mon Jan 28 09:33:30 EST 2002
"Alex Martelli" <aleax at aleax.it> wrote:
> "Pedro Rodriguez" <pedro_rodriguez at club-internet.fr> wrote in message
> news:pan.2002.01.28.11.53.09.55635.1770 at club-internet.fr...
> ...
>> Borg vs. Singleton : I was just kiding, sort of... I wonder if it would
>> be good to provide a diagram when dealing with new concepts. Example :
>
> For people who "thing visually", maybe.
A diagram also helps to capture an intention.
> ... I prefer to think verbally
> (sharing state vs sharing identity).
>
Verbally ? Never would have thought that of you ;)
Still, phrases are always required, a diagram won't explain all
(I have something like 'the map is not the field' in mind).
> Back to the mechanics (metaclasses or otherwise) that one can use to
> implement creational design patterns (and other kinds). When I have a
> class object C, "instantiation" is a call on C, say:
> instance = C(*args, **kwds)
> so it involves C.__call__, which typically comes from C.__class__.
^^^^^^^^^^
< example sniped for brievety >
Ok. Except for a probable typo on 'C.__call__' which may exist or not but
is not involved in this process (your examples suggests C.__new__).
> It's easy to check this (Python's GREAT this way...):
>
> class X(object):
> def __new__(self, *args, **kwds):
> result = object.__new__(self)
> print 'X.__new__(%r,%s,%s)->%s'%(self, args, kwds, result)
> return result
> def __init__(self, *args, **kwds):
> print 'X.__init__(%r,%s,%s)'%(self, args, kwds)
>
> x=X('ba','be',bi='bo')
>
> D:\>python ni.py
> X.__new__(<class '__main__.X'>,('ba', 'be'),{'bi': 'bo'})-><__main__.X
> object at
> 0x00789C50>
> X.__init__(<__main__.X object at 0x00789C50>,('ba', 'be'),{'bi': 'bo'})
>
>
> Nothing mysterious here, right? Just notice the difference between
> 'self' in the two cases. Now, clearly, if you want to influence what
> happens when C is called, your options include overriding __new__ and/or
> __init__ in C, and overriding __call__ in C's type (metaclass).
>
> If you work in C.__init__, then programmers who subclass C must call
> C.__init__ explicitly (if they define their own __init__, as most often
> happens). Similarly for __new__, although I guess it's much rarer for
> __new__ to be overridden in subclasses. In any case, your control is
> limited, since the "template method" type.__call__ remains untouched.
>
It may be good to remind that __new__ is a static method whose signature
looks like 'def __new__(cls, ...)', and also, I felt confused when reading :
> If you choose to work by subclassing type (having C use your own
> metaclass), you can get at the "template method" itself and insert or
> tweak whatever you need to. For example:
>
> class autoborg(type):
> metastate = {}
> def __call__(self, *args, **kwds):
> result = self.__new__(self, *args, **kwds)
> result.__dict__ = self.metastate
> if result: result.__init__(*args, **kwds)
> return result
>
... because, I would have written :
result = type.__new__(self, *args, **kwds)
instead of :
result = self.__new__(self, *args, **kwds)
I just realize now that 'parentClass.staticMethod()' is equivalent to
'self.staticMethod()' (same arguments used), whereas in classical classes
the meaning is different (bound/unbound method).
< sniping rest of fine explanations>
I guess I need a clearer head to sort out this __call__/__new__/__init__
chain. I wonder how much time it will take me to get my marks on those
matters, and use them when appropriate (headaches in sight ;)
>
> By the way, I have a new counter for those who are horrified by the idea
> of naming Borg after a TV show -- "TV show, what TV show? It's named in
> honor of Jorge Luis Borges, master of identity and separateness, just
> shortened for ease of use and uniformity of pronunciation across
> linguistic boundaries...". [And if you buy THIS, I have a nice bridge to
> sell you, and some very promising land in Florida, and...]
>
I like it (not the Borges reference, whose writings I recommend, just like
those from Bioy Casares), it just these words 'just shortened'. If it
wasn't coming from you, I might have give it a thought ;)
Well... actually, I did it, just to show how naive I am ;)
Regards,
--
Pedro
More information about the Python-list
mailing list