return an object of a different class

alex23 wuwei23 at
Fri Feb 18 02:59:38 CET 2011

Jean-Michel Pichavant <jeanmic... at> wrote:
> You simply don't return inconsistent types with a return statement. This
> is a general rule in programming that has probably exceptions but
> regarding what you're saying, you clearly don't want to do that.

I don't think they were intended to be inconsistent types, but
subclasses of the same type. Returning different subclasses is exactly
what a factory is for. And given Python's propensity for duck typing,
they don't even really need to be subclassed for the same object, they
just need the interface that is required.

> Immagine the following code:
> oNumber = MyNumbers( # note that oNumber is not a
> MyNumbers instance... quite confusing don't you think ?
> oNumber. ... wait a minute, which methods am I allowed to call ???
> SmallNumbers adn BigNumbers have differents methods.

What if they have different implementations rather than methods?

More information about the Python-list mailing list