How do I add a method to a class from a third-party Python module without editing the original module
Question: I'm using the `Basemap` object from `basemap` module in the matplotlib toolkit
(`mpl_toolkits.basemap.Basemap`). In `basemap`'s `__init__.py` file (i.e. the
`mpl_toolkits.basemap.__init__` module), a method `drawparallels` is defined
which draws latitudes on the map. I aim to duplicate that method to make a new
method called `drawmlat`, making some adjustments in order to plot magnetic
latitudes instead of geographic latitudes.

Ideally, I want the new `drawmlat` to be equivalent to the original
`drawparallel` (a bound method of the instances of Basemap that I can call
with using `BasemapInstance.drawmlats()`), and I do not want to modify the
original file. How would I accomplish this?

I have tried variations of the "recipe" `MyObj.method = MethodType(new_method,
None, MyObj)`, but without placing anything in the original source file, the
new method does not have access to globals etc. from the Basemap module (e.g.
defined in its `__init__.py`).

If it seems I have misunderstood something, I probably have - I am more or
less completely new to object-oriented programming.


Answer: Python is highly modifiable. Just add your function to the _class_ :

    
    
    from mpl_toolkits.basemap import Basemap
    
    
    def drawmlat(self, arg1, arg2, kw=something):
        pass
    
    Basemap.drawmlat = drawmlat
    

Now the `Basemap` class has a `drawmlat` method; call it on instances and
`self` will be bound to the instance object. When looking up the method on
instances, the function will automatically be bound as a method for you.

Anything defined in the `Basemap.__init__` method that you need to care about
are attributes on `self`.

Having looked over the `mpl_toolkits.basemap.__init__` module, I do see that
the `drawparallel` method relies on a _few_ globals; you can import those from
the module into your own namespace:

    
    
    from mpl_toolkits.basemap import Basemap, _cylproj, _pseudocyl
    

This is no different from other imports you'd make; the original
`drawparallel` method also relies on `import numpy as np` and `from
matplotlib.lines import Line2D`, which make both `np` and `Line2D` globals in
the original module.

