Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的自定义Python枚举器类型会在Eclipse中导致错误_Python_Eclipse - Fatal编程技术网

为什么我的自定义Python枚举器类型会在Eclipse中导致错误

为什么我的自定义Python枚举器类型会在Eclipse中导致错误,python,eclipse,Python,Eclipse,我定义了一个自定义类型,在Python中用作枚举器 # enums.py def enum(**enums): return type('Enum', (), enums) UserType = enum( USER=1, POWER=2, ADMIN=3 ) 我使用的枚举器如下所示: from my_app import enums if user.type_id == enums.UserType.ADMIN: print "User is A

我定义了一个自定义类型,在Python中用作枚举器

# enums.py

def enum(**enums):
    return type('Enum', (), enums)

UserType = enum(
    USER=1,
    POWER=2,
    ADMIN=3
)
我使用的枚举器如下所示:

from my_app import enums

if user.type_id == enums.UserType.ADMIN:
    print "User is Admin"
代码按预期完美运行,但interperator给了我以下错误:

导入的未定义变量:ADMIN


有人知道这是为什么以及我如何修复它吗?

在我的环境中,它运行正常


可能是因为python是弱类型语言,所以编译器不知道enums.UserType是否具有属性“ADMIN”,所以它给出了错误消息。但是在运行时环境中,这没关系。

谢谢您的回复。你在Windows上使用eclipse吗?是的,几年前我在Windows上使用了eclipse的PyDev插件。但现在改为PYcharm,它真的很迷人,值得一试。