[Python] Python: How to Type Hint a Class Argument in a Static Method Python? [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Let's say I have a class Circle with the following definition:

    class Circle:
    def __init__(self, r, _id):
    self.r = r
    self.id = _id

    def area(self):
    return math.pi * (self.r ** 2)


    I want to write a function that compares two circles and returns the id of the smallest one

    def compare_circles(circle_1: Circle, circle_2: Circle) -> str:
    if circle_1.r < circle_2.r:
    return circle_1.id
    else:
    return circle_2.id


    I would like to place this method as a static method on the class. (Is this a bad idea?)

    class Circle:

    def __init__(self, r, _id):
    self.r = r
    self.id = _id

    def area(self):
    return math.pi * (self.r ** 2)

    @staticmethod
    def compare_circles(circle_1: Circle, circle_2: Circle) -> str:
    if circle_1.r < circle_2.r:
    return circle_1.id
    else:
    return circle_2.id


    However this does not work because the Circle object is not available to be type hinted. I have to remove the type hints in the method. Using a classmethod also does not work, as the cls argument is not available to the other method arguments.

    Continue reading...

Compartilhe esta Página