【问题标题】:How can I access a constant of an inner class from a function definition?如何从函数定义中访问内部类的常量?
【发布时间】:2018-09-26 05:43:42
【问题描述】:

我有类sprite 和内部类type

class sprite:
    class type:
        ThisValue = 10
        OtherValue = 22

    def __init__(self, type = sprite.type.ThisValue):
        self.type = type

当我尝试这个时,它说名称 sprite 没有定义;错误在def __init__() 行上引发。如何在函数定义中访问ThisValue?有没有更好的方法来编写type 类?

【问题讨论】:

    标签: python class subclass


    【解决方案1】:

    你只需要

    def __init__(self, type = type.ThisValue):
    

    【讨论】:

    • 谢谢,我傻了没想到哈哈
    • @Tanner H。请注意,类和参数的相同名称通常是个坏主意。最好将 class type 重命名为 class type_cls 或重命名方法参数。
    猜你喜欢
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2022-01-09
    相关资源
    最近更新 更多