{{lastupdated_at}} by {{lastupdated_by}} {{>toc}} h1. How to create a cast in Python? Ideally, one would like to avoid casts, as they indicate a poorly defined interface. However, sometimes one still would like to have a cast. An example is the @GInstDir@ class, which defines an abstract direction in the instrument system. Sometimes, the direction has nothing to do with a position on the sky (for example for the SPI telescope on INTEGRAL which is a non-imaging telescope), sometimes the instrument direction is identical to a sky position (for all imaging telescopes). So how can we get out of @GInstDir@ object the sky direction? We need a cast. A cast can be implement as a class extension in the SWIG interface. Here a cast for the @GCTAInstDir@ class:

%extend GCTAInstDir {
    GCTAInstDir(GInstDir* dir) {
        GCTAInstDir* ptr = dynamic_cast(dir);
        if (ptr != NULL) {
            return (ptr->clone());
        }
        else {
            throw GException::bad_type("GCTAInstDir(GInstDir*)", "GInstDir not of type GCTAInstDir");
        }
    }
};
It is used for example as in
instdir = GCTAInstDir(dir)
Note that the extension does in fact not really implement a cast, but it provides a deep copy of the instrument direction. The method also verifies that the cast is valid.