【问题标题】:python how to access class variables inside initpython如何访问init中的类变量
【发布时间】:2023-02-15 22:46:24
【问题描述】:

我想访问 init 中的类变量

class Hare:
    APP = 10;

    def __init__(self):
        print(APP)

h = Hare()

它给出错误

!!! NameError: name 'APP' is not defined

【问题讨论】:

  • 您是要使用 self.APP = 10print(self.APP) 吗?另外,分号是技术上在 Python 中有效,但您可以将其省略。
  • self.APPHare.APP

标签: python


【解决方案1】:

你可能想要:

class Hare:
    APP = 10

    def __init__(self):
        print(self.APP) # 10

h = Hare()

【讨论】:

    猜你喜欢
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多