"Scott David Daniels" <[EMAIL PROTECTED]> skrev i meddelandet news:[EMAIL PROTECTED] > BrJohan wrote: ... >> is it then possible to have this call: >> obj = A(data) >> return an instance of that particular class (e.g. class C3) in the >> hierarchy that - as decided by the __new__ functions - is the 'correct' >> one? >> >> A.__new__ could select between A, B1 and B2, while B1.__new__ could >> choose from B1, C3 and C4. >> >> I know how to use a class factory - and could work around using such a >> mechanism. However I am interested to know if I could let the classes do >> the work by themselves. > > Yes, it can be done. Yes, it is unclear (and hence UnPythonic). > The class factory _is_ the straightforward way to do this. The > following is the workaround (if you have to maintain A(...)): > > > 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) > > > --Scott David Daniels > [EMAIL PROTECTED]
Agreed that the class factory method most often (maybe always) is the best one. For certain reasons, and in this particular case, I prefer the UnPythonic way. Sometimes it's good to have "more than one way to do it". It was the "return object.__new__(class_) " that I did not came to think of myself, that did it. Thank you for yor helpfulness. BrJohan -- http://mail.python.org/mailman/listinfo/python-list