-1

I want to know Python type comment for function argument.

I already know basic type comment. (int, str, list, dict ... etc ...)

but. I don't know how to Custom Class type comment in This Class

ex)

class Student():
    def __init__(self, score:int):
        self.score = score
    def compare_score(self, another_student: Student):
        return 1 if self.score > another_student.score else 0 if self.score == another_student.score else -1

this situation. receive an error message => Unresolved reference 'Student'

plz help.

snow_kor
  • 1
  • 1
  • Custom class type as in forward referencing or you want operator overloading for comparison operators? check about forward referencing https://stackoverflow.com/questions/2035423/how-to-refer-to-the-class-from-within-it-like-a-recursive-function – SobyDamn Sep 02 '22 at 05:57
  • 1
    @SobyDamn type annotations are a special case here. I linked the appropriate canonical duplicate. – Karl Knechtel Sep 02 '22 at 06:20
  • @KarlKnechtel this is much better thanks. – SobyDamn Sep 02 '22 at 06:24

1 Answers1

0

Your using self class. better you just create external function for this

class Student():
    def __init__(self, score:int):
        self.score = score
        
def compare_score(self, another_student: Student):
        return 1 if self.score > another_student.score else 0 if self.score == another_student.score else -1

or just pass value you need to compare

class Student():
    def __init__(self, score:int):
        self.score = score
    def compare_score(self, score:int):
        return 1 if self.score > score else 0 if self.score == score else -1
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
  • This example is Simple example.... I'm sorry for your misunderstanding. and, I'm studing typing now. so function arguement change is not answer for me.(although This is wonderful Solution. Thank you) – snow_kor Sep 02 '22 at 06:12