Question regarding __new__
Hi, the behaviour I always observed when creating instances by calling the class A is that '__init__' is always only called when the object returned by A.__new__ is an instance of A. This can be observed by the following code: class A(object): def __new__(cls, *args, **kwds): print "A.__new__", args, kwds return object.__new__(B, *args, **kwds) def __init__(cls, *args, **kwds): print "A.__init__", args, kwds class B(object): def __new__(cls, *args, **kwds): print "B.__new__", args, kwds return object.__new__(cls, *args, **kwds) def __init__(cls, *args, **kwds): print "B.__init__", args, kwds Interactively A() then gives: >>> A() A.__new__ () {} <__main__.B object at 0xb7bed0ec> Yet [1] says: "[...] some rules for __new__: [...] If you return an object of a different class, its __init__ method will be called." Am I missing something? Is this documented somewhere else? Also it would be nice if someone could point me to the function that implements this in C. I didn't find anything in object.c or typeobject.c. Best regards Frank Benkstein. [1] http://www.python.org/download/releases/2.2/descrintro/#__new__ -- GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51 signature.asc Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: challenge ?
Hi, On 22 Mar 2007 09:41:43 -0700 "alain" <[EMAIL PROTECTED]> wrote: > I have a problem I wonder if it has been solved before. > I have a dictionnary and I want the values in the dictionnary to be > annotated with the rank that would be obtained by sorting the values > > def annotate_with_rank(my_dict): > > return my_annotated_dict > > In other words, any value a_value would become a 2-tuple > (a_value,rank_of_a_value) > > I seek an elegant solution. In your specification of the problem it is unclear what should be done with duplicate values. My solution assigns every value a different rank (starting from 0) such that the highest rank is len(my_dict) - 1. def annotate_with_rank(my_dict): items = my_dict.items() items.sort(key = lambda (k, v): v) return dict((k, (i, v)) for i, (k, v) in enumerate(items)) Best regards, Frank Benkstein. -- GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51 signature.asc Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: challenge ?
Hi, again, On Thu, 22 Mar 2007 18:11:46 +0100 Frank Benkstein <[EMAIL PROTECTED]> wrote: > On 22 Mar 2007 09:41:43 -0700 > "alain" <[EMAIL PROTECTED]> wrote: > > > I have a problem I wonder if it has been solved before. > > I have a dictionnary and I want the values in the dictionnary to be > > annotated with the rank that would be obtained by sorting the values > > > > def annotate_with_rank(my_dict): > > > > return my_annotated_dict > > > > In other words, any value a_value would become a 2-tuple > > (a_value,rank_of_a_value) > > > > I seek an elegant solution. > > In your specification of the problem it is unclear what should be done > with duplicate values. My solution assigns every value a different > rank (starting from 0) such that the highest rank is len(my_dict) - 1. The two other possibilities were to still make len(my_dict) ranks but assign equal values an equal rank. That would mean that some ranks are untaken. Or, lastly, to make only as much ranks as there are unique values. > def annotate_with_rank(my_dict): > items = my_dict.items() > items.sort(key = lambda (k, v): v) > return dict((k, (i, v)) for i, (k, v) in enumerate(items)) def annotate_with_rank_2(my_dict): values = my_dict.values() values.sort() return dict((k, (values.index(v), v)) for k, v in my_dict.iteritems()) def annotate_with_rank_3(my_dict): values = list(set(my_dict.itervalues())) values.sort() return dict((k, (values.index(v), v)) for k, v in my_dict.iteritems()) Best regards, Frank Benkstein. -- GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51 signature.asc Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
PEP 3115 in Python 2.6?
Hi, is there any possibility to see PEP 3115 implemented for Python 2.6? AFAICS there's nothing about it that should limit it's implementation to Python 3000 if backwards compatibility with '__metaclass__' is preserved. At least the '__prepare__' in metaclasses method would be very useful. Keyword parameters to the class statement are not so important IMHO. Best regards Frank Benkstein. -- GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51 signature.asc Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list