属性错误:没有属性';completeKey';-python

属性错误:没有属性';completeKey';-python,python,Python,运行此代码时出现以下错误: 属性错误:DisplayWelcome没有属性“completeKey” import controller.game_play import cmd class DisplayWelcome(cmd.Cmd): """Welcome user to game""" def __init__(self): self.do_greet() prompt = 'ENTER'

运行此代码时出现以下错误:

属性错误:DisplayWelcome没有属性“completeKey”

import controller.game_play
    import cmd

    class DisplayWelcome(cmd.Cmd):
        """Welcome user to game"""
        def __init__(self):
            self.do_greet()

        prompt = 'ENTER'
        intro = '\n'.join(['        Welcome To   ',
         '...ZOMBIE IN MY POCKET...'])

        def do_greet(self):
            print ('        Welcome To   ')
            print ("...ZOMBIE IN MY POCKET...")

        def do_inform(self, line):
            k = input('Enter a letter')
            print (k)


    def main():
        d = DisplayWelcome()
        #d.do_greet()
        d.cmdloop()
        s = controller.game_play.stuff()

    if __name__ == '__main__':
        main()
这是一个简单的…;-) 您忘记调用父类的构造函数(cmd.cmd)。在这里,completekey属性会自动声明为默认值。这就解决了问题

import controller.game_play
import cmd

class DisplayWelcome(cmd.Cmd):
    """Welcome user to game"""
    def __init__(self):

        #### THIS IS THE LINE YOU FORGOT!!!!
        super(DisplayWelcome, self).__init__()
        # or cmd.Cmd.__init__(self)


        self.do_greet()

    prompt = 'ENTER'
    intro = '\n'.join(['        Welcome To   ',
     '...ZOMBIE IN MY POCKET...', '  Created by Ben Farquhar   '])

    def do_greet(self):
        print ('        Welcome To   ')
        print ("...ZOMBIE IN MY POCKET...")
        print ("  Created by Ben Farquhar   ")

    def do_inform(self, line):
        k = input('Enter a letter')
        print (k)


def main():
    d = DisplayWelcome()
    #d.do_greet()
    d.cmdloop()
    s = controller.game_play.stuff()

if __name__ == '__main__':
    main()

@njzk2在一个名为cmd.py的系统库中。错误不在您发布的代码中。发布回溯,加上其中提到的代码。这不是离题,甚至不是太广泛。它帮助了我。cmd是一个默认的python库,它是一个手动页面sugests扩展库,我不认为这是一个离题的问题,只是一个需要改进的问题。哈维尔提供的答案实际上帮我省去了一个大麻烦。它产生的错误无疑与我的类似:File“/usr/lib/python3.5/cmd.py”,第106行,如果self.use\rawinput和self.completekey:AttributeError:“CmdDef”对象没有属性“completekey”,那么在
super()
上调用init会比在
cmd.cmd
上调用init要好,非常感谢!同样的麻烦!在Python2.x中,
cmd.cmd
是一个旧式类,因此
super
无法工作。(请参阅)谢谢,super(类名,self)。\uuuu init\uuuuuuu()对我来说(3.5)太完美了。它对我有用。!