Unpythonic? Impossible??

Scott David Daniels scott.daniels at acm.org
Sun Mar 19 19:57:30 CET 2006


Felipe Almeida Lessa wrote:
> Em Dom, 2006-03-19 às 08:54 -0800, Scott David Daniels escreveu:
>>      class A(object):
>>          def __new__(class_, *args, **kwargs):
>>              if class_ is A:
>>                  if want_a_B1(*args, **kwargs):
>>                      return B1(*args, **kwargs)
>>                  elif want_a_B2(*args, **kwargs):
>>                      return B2(*args, **kwargs)
>>              return object.__new__(class_) # Use *a,... except for object
>>
>>      class B1(A):
>>          def __new__(class_, *args, **kwargs):
>>              if class_ is B1:
>>                  if want_a_B1(*args, **kwargs):
>>                      return B1(*args, **kwargs)
>>                  elif want_a_B2(*args, **kwargs):
>>                      return B2(*args, **kwargs)
>>              return super(B1, class_).__new__(class_, *args, **kwargs)
> 
> Why you have that if on B1.__new__? B1 will be created only by
> A.__new__, which already did a check.

Of course your are right.  It needs to be more like:
       class B1(A):
           def __new__(class_, *args, **kwargs):
               if class_ is B1:
                   if want_a_C1(*args, **kwargs):
                       return C1(*args, **kwargs)
                   elif want_a_C2(*args, **kwargs):
                      return C1(*args, **kwargs)
               return super(B1, class_).__new__(class_, *args, **kwargs)


--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list