Say that I have defined a class and I make a couple of instances of it:
A = MyClass()
B = MyClass()
Assume now that I defined a my_plot
function that takes instances of classes MyClass
as input, e.g. I have something like the following:
def my_plot(X,Y):
# Do something and plot
plt.legend([?,??])
that I can call with my_plot(A,B)
.
I wish to replace the ?
and the ??
in the line plt.legend([?,??])
of the pseudo-code snippet above with with A
and B
, respectively.
So far, a way to circumnavigate the problem is to equip the MyClass
with an attribute name
and do something like this
A = MyClass('nameA')
B = MyClass('nameB')
and then
def my_plot(X,Y):
# Do something
plt.legend([X.name,Y.name])
but I found boring to to instantiate a class with A = MyClass('nameA'), B = MyClass('nameB')
. I wish to instantiate my classes with A = MyClass(), B = MyClass()
and retrieve the instances names for the plot inside the my_plot
function.