ライブラリのクラスがあり、コンストラクターの動作を変更したいと考えています。
私がやっている:
from library import TheClass
TheClass._original_init = TheClass.__init__
def f(self, *karg, **kargw):
print "hello"
self._original_init(*karg, **kargw)
TheClass.__init__ = f
それは正しいアプローチですか?この特定のケースで問題があります:
import matplotlib.pyplot as plt
from matplotlib import axes
from matplotlib.ticker import MultipleLocator
axes.Subplot._original_init = axes.Subplot.__init__
def f(self, *karg, **kargw):
def add_minor_locator(axis, n = 5):
ticklocs = axis.get_ticklocs()
diff = ticklocs[1] - ticklocs[0]
minorLocator = MultipleLocator(diff/float(n))
axis.set_minor_locator(minorLocator)
add_minor_locator(self.get_xaxis())
add_minor_locator(self.get_yaxis())
self._original_init(*karg, **kargw)
axes.Subplot.__init__ = f
fig, ax = plt.subplots(nrows=1, ncols=1)
私は得る:
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1319, in get_xaxis
return self.xaxis
AttributeError: 'AxesSubplot' object has no attribute 'xaxis'