Opierając się na @ Godsmith na train of thought i adresowanie @ potrzebie Zitrax jest (myślę), aby wykonać kopiowanie danych dla wszystkich atrybutów w konstruktorze:
class ConfusionMatrix(pd.DataFrame):
def __init__(self, df, *args, **kwargs):
try:
# Check if `df` looks like a `ConfusionMatrix`
# Could check `isinstance(df, ConfusionMatrix)`
# But might miss some "ConfusionMatrix-elligible" `DataFrame`s
assert((df.columns == df.index).all())
assert(df.values.dtype == int)
self.construct_copy(df, *args, **kwargs)
return
except (AssertionError, AttributeError, ValueError):
pass
# df is just data, so continue with normal constructor here ...
def construct_copy(self, other, *args, **kwargs):
# construct a parent DataFrame instance
parent_type = super(ConfusionMatrix, self)
parent_type.__init__(other)
for k, v in other.__dict__.iteritems():
if hasattr(parent_type, k) and hasattr(self, k) and getattr(parent_type, k) == getattr(self, k):
continue
setattr(self, k, deepcopy(v))
Ten ConfusionMatrix
klasa dziedziczy pandas.DataFrame
i dodaje mnóstwo innych Attribut es i metody, które muszą zostać przeliczone, chyba że dane macierzy other
można skopiować. Szukając rozwiązania, znalazłem to pytanie.
Możesz być zainteresowany w tej kwestii [Disclaimer: był ten, który go o to poprosił]: http://stackoverflow.com/questions/990758/reclassing-an-instance-in-python – balpha
Bądź ostrożny. Ostrzeżenia zamieszczone przez niektórych odbierających nie mogą być niedoceniane. – balpha
Do tego nie wygląda na bardzo czytelny, prawdopodobnie zmienię mój kod z używania dziedziczenia, aby zamiast niego zamykać tylko drugi obiekt. – Zitrax