1


I want to extend my python class with the pyplot module from matplotlib:

import matplotlib.pyplot as plt

class MyClass(object, plt):
    pass

This is not working:

TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

I found this question, but actually, it didn't really helped me out. I search with

>>> type(matplotlib)
>>> type(matplotlib.pyplot)

etc. for classes, but I only get modules. The API documentation doesn't says anything about a class, too. So, what am I doing wrong? I'm still very new to python...

Thank you in advance!
-ju-

Community
  • 1
  • 1
ju.
  • 304
  • 3
  • 16
  • *"I want to extend my python class with the pyplot module from matplotlib"* - why? What are you trying to achieve here? – jonrsharpe Mar 12 '15 at 13:48
  • I want to extend the pyplot module with a few features I frequently use with pyplot. – ju. Mar 12 '15 at 13:50
  • So, again, why do you want to inherit from it? Why not just create your own module that imports `pyplot` and defines the additional functionality you want? – jonrsharpe Mar 12 '15 at 13:51
  • To be honest, I never thougth about that... or about the difference between classes and modules. As I said, I'm new to python and only got a (hopefully not too) little understanding of oop from other languages. – ju. Mar 12 '15 at 13:54
  • This question is not good as it is: it canbe answered, but the answers will tell you some details about how Python modules and class work, but won't get yo any closer to what you really want to do. Please, make anther question to ask what do you want to achieve in the end (I want the ability to have a single call to do this and that). SOmeone should be writting what is wrong with the code above, and why it does not work shortly - but that will hardly help you. – jsbueno Mar 12 '15 at 13:55
  • 1
    Perhaps you should get a better grip of Python's syntax and object model before trying something so complex? – jonrsharpe Mar 12 '15 at 13:55
  • No reason to be rude. I'm open minded to learn that stuff, but the best is learning by doing. Starting with classes is the next step in learning python, and this includes inheration. – ju. Mar 12 '15 at 14:04
  • "inheritance" - :-) and believe us - you would not like to see the rude comments here - although the one above does not sound polite, it is fair. I think the comment author did not mean that words in a harmful way. – jsbueno Mar 12 '15 at 14:07

1 Answers1

0

The nature of the error you are getting is simply because you can't have a class inherit from both "object" and matplotlib.pyplot - but them, pyplot is a module - That is: an instance of the "ModuleType" class - and it makes no sense to try to augment it with inheritance.

You could augment pyplot with simple attribute assignments:

import matplotlib.pyplot

def my_new_plot_function(...):
    ...

matplotlib.pyplot.new_func = my_new_plot_function

and your new function would be available for other modules that make use of the pyplot module. However, as stated in the comments, there is no need to do that - and that is definitely not desirable from an architectural point of view. You can even replace one of the existing functions or classes in the pyplot module doing that - this is called monkey patching - and you will find it to be higly unreliable unless you really, really know what you are doing - and even so, poorly maintainable, and an obscure feature in your system as a whole.

Now- there are classes inside the Pyplot module - you could inherit from those, and enhance their behavior - and you would import your inherited classes in your code, from your own module. Maybe inheritance is not the best method to add upon their behavior in modern code (aggregation and proxying of the needed methods is preferred) - but that would work.

The classes that can be subclassed are:

>>> [obj for obj in dir(pyplot) if isinstance(getattr(pyplot, obj), type)]
['Annotation', 'Arrow', 'Artist', 'AutoLocator', 'Axes', 'Button', 'Circle', 'Figure', 'FigureCanvasBase', 'FixedFormatter', 'FixedLocator', 'FormatStrFormatter', 'Formatter', 'FuncFormatter', 'GridSpec', 'IndexLocator', 'Line2D', 'LinearLocator', 'Locator', 'LogFormatter', 'LogFormatterExponent', 'LogFormatterMathtext', 'LogLocator', 'MaxNLocator', 'MultipleLocator', 'Normalize', 'NullFormatter', 'NullLocator', 'PolarAxes', 'Polygon', 'Rectangle', 'ScalarFormatter', 'Slider', 'Subplot', 'SubplotTool', 'Text', 'TickHelper', 'Widget', 'silent_list']
jsbueno
  • 99,910
  • 10
  • 151
  • 209