How can I get a reference to the class in a static method?
I have following code:
class A:
def __init__(self, *args):
...
@staticmethod
def load_from_file(file):
args = load_args_from_file(file)
return A(*args)
class B(A):
...
b = B.load_from_file("file.txt")
But I want to B.load_from_file return object of type B, not A. I know if load_from_file wouldn't be a static method I could do
def load_from_file(self, file):
args = load_args_from_file(file)
return type(self)__init__(*args)
This is what classmethods are for; they're like staticmethod in that they don't rely on instance info, but they do provide info about the class it was invoked on by providing it implicitly as the first argument. Just change your alternate constructor to:
@classmethod # class, not static method
def load_from_file(cls, file): # Receives reference to class it was invoked on
args = load_args_from_file(file)
return cls(*args) # Use reference to class to construct the result
When B.load_from_file is invoked, cls will be B, even though the method is defined on A, ensuring you construct the correct class.
In general, any time you find yourself writing alternate constructors like this, you always want a classmethod to enable inheritance properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With