蓝大伟 发表于 2020-8-13 07:31:44

出错啦!

class Player:
    instance = None

    init_flag = False

    def __new__(cls,yy):

    def __init__(self):
      if Player.init_flag:
            return
      print("初始化")
      Player.init_flag = True
p1 = Player()
print(p1)
p2 = Player()
print(p2)


File "C:\Users\tlan2\PycharmProjects\david\jj.py", line 8
    def __init__(self):
    ^
IndentationError: expected an indented block

Process finished with exit code 1

怎么解决

老八秘制 发表于 2020-8-13 07:49:14

缩进删了重打试试

1q23w31 发表于 2020-8-13 07:54:24

class Player:
    instance = None
    init_flag = False
    def __new__(cls,yy):
      pass

    def __init__(self):
      if Player.init_flag:
            return
      print("初始化")
      Player.init_flag = True

heidern0612 发表于 2020-8-13 08:20:55

IndentationError: expected an indented block

IndentationError 看到这个不用想,就是缩进错误,记住就好。

革命年 发表于 2020-8-13 22:23:24

1q23w31 发表于 2020-8-13 07:54


new方法要返回对象!!

1q23w31 发表于 2020-8-14 07:06:38

革命年 发表于 2020-8-13 22:23
new方法要返回对象!!

那你要把new写完整啊,什么都不写,定义不完全,当然报错
页: [1]
查看完整版本: 出错啦!