Unpythonic? Impossible??

B

BrJohan

Assume having this class hierarchy: (in principle and without details)
class A(object):
class B1(A):
class B2(A):
class C1(A1):
class C2(A1):
class C3(B1):
class C4(B2):

each of those classes have an initializer __init__(self, data): and an
overloaded __new__(cls, data):

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.

/BJ
 
S

Scott David Daniels

BrJohan said:
Assume having this class hierarchy: (in principle and without details)
class A(object):
class B1(A):
class B2(A):
...
each of those classes have an initializer __init__(self, data): and an
overloaded __new__(cls, data):

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
(e-mail address removed)
 
F

Felipe Almeida Lessa

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.
 
B

BrJohan

Scott David Daniels said:
BrJohan wrote: ....

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
(e-mail address removed)

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
 
S

Scott David Daniels

Felipe said:
Em Dom, 2006-03-19 às 08:54 -0800, Scott David Daniels escreveu:

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
(e-mail address removed)
 
E

Erik Max Francis

BrJohan said:
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.

You can, but a class factory is going to be much clearer and probably
more maintainable. From the user's perspective, there's no difference
from calling a class A to instantiate it, and calling a factory function
called A that selects the appropriate class and returns an instance of it.
 
P

Paul Rubin

Erik Max Francis said:
You can, but a class factory is going to be much clearer and probably
more maintainable. From the user's perspective, there's no difference
from calling a class A to instantiate it, and calling a factory
function called A that selects the appropriate class and returns an
instance of it.

I remember having a similar problem involving multiple base classes
and deciding that factory functions couldn't do quite what I wanted.
Here's a thread about it, with a recipe using metaclasses by Roeland
Rengelink:

http://tinyurl.com/rz6ne

Unfortunately, the subtleties of what I was trying to do now escape
me.
 
E

Erik Max Francis

Paul said:
I remember having a similar problem involving multiple base classes
and deciding that factory functions couldn't do quite what I wanted.
Here's a thread about it, with a recipe using metaclasses by Roeland
Rengelink:

http://tinyurl.com/rz6ne

Unfortunately, the subtleties of what I was trying to do now escape
me.

Your objection seemed to be that you'd prefer that you not have to have
an explicit if/elif... (or lookup table) since that didn't seem very OO.
Any solution that accomplishes this, whether it's a factory function,
metaclasses, or a more traditional type of "virtual constructor" (your
proposal at the end of your post, which is likely how you'd do it in
C++) all need this.

The short version of all this is all these approaches will work, are
Functionally equivalent from the user's perspective, and all require
upkeep, but some require more upkeep than others. In a dynamic language
like Python, the best solution is the most straightforward one that
requires the least upkeep. And that's a factory pattern.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top