Down casting Python objects

Andrey Fedorov anfedorov at gmail.com
Wed Mar 10 08:31:14 EST 2010


On Wed, Mar 10, 2010 at 12:24 AM, Rami Chowdhury <rami.chowdhury at gmail.com>
wrote:

> Could you tell us *why* you need to down-cast x? Explicit type-casting is
> usually unnecessary in Python...


Sure! It's related to the longer question I unwisely asked during PyCon [1]
(when no one had time to read it, I suppose).

I have a couple of different flavors of request objects which I'd like to
make conform to a single interface. So Request `x' come in, I determine
which kind of request I'm dealing with, and would like to "down-cast" it to
ARequest or BRequest, classes which will provide appropriate accessor
properties for the rest of the code to use.

An option clearly in line with Python's docs might be for `x' to be an
attribute of an ARequest instance, but that would complicate the code of
ARequest. What I'm looking for is a way of adding mix-in's at runtime, if
that makes sense.

Cheers,
Andrey

1. http://mail.python.org/pipermail/python-list/2010-February/1236681.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100310/24c796af/attachment.html>


More information about the Python-list mailing list