首页 > 解决方案 > 用于静态方法参数的 Python 集类

问题描述

尝试为静态方法参数指定类时,例如:

class A:
    @staticmethod
    def staticm(param1: A):
        return 1

它引发了一个错误,即'A' is not defined.

为什么我不能这样做?有漏洞吗?

我可以检查isinstance...但是为什么在python中不允许这样做?

标签: pythonclassstatic-methods

解决方案


这个答案基于@saquintes 的评论。

解决方案:将类添加A为字符串'A'

class A:
    @staticmethod
    def staticm(param1: 'A'):
        return 1

推荐阅读