Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to get reference to class in static method [duplicate]

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)
like image 983
Jara M Avatar asked Jun 19 '26 16:06

Jara M


1 Answers

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.

like image 173
ShadowRanger Avatar answered Jun 21 '26 06:06

ShadowRanger