Python 类继承属性错误 - 为什么?怎么修?

2024-01-11

SO 上的类似问题包括:this one https://stackoverflow.com/questions/10268603/python-class-inheritance-attributeerror-subclass-object-has-no-attribute and this https://stackoverflow.com/questions/927985/python-class-inheritance-issue。我还阅读了我能找到的所有在线文档,但我仍然很困惑。我将非常感谢你的帮助。

我想在我的 CastSpell 类 lumus 方法中使用 Wand 类 .wandtype 属性。但我不断收到错误“AttributeError:‘CastSpell’对象没有属性‘wandtype’。”

这段代码的工作原理:

class Wand(object):
    def __init__(self, wandtype, length):
        self.length = length 
        self.wandtype = wandtype

    def fulldesc(self):
        print "This is a %s wand and it is a %s long" % (self.wandtype, self.length) 

class CastSpell(object):
    def __init__(self, spell, thing):
        self.spell = spell 
        self.thing = thing

    def lumus(self):
        print "You cast the spell %s with your wand at %s" %(self.spell, self.thing) 

    def wingardium_leviosa(self): 
        print "You cast the levitation spell."

my_wand = Wand('Phoenix-feather', '12 inches') 
cast_spell = CastSpell('lumus', 'door') 
my_wand.fulldesc()  
cast_spell.lumus() 

这段代码尝试了继承,但没有。

class Wand(object):
    def __init__(self, wandtype, length):
        self.length = length 
        self.wandtype = wandtype

    def fulldesc(self):
        print "This is a %s wand and it is a %s long" % (self.wandtype, self.length) 

class CastSpell(Wand):
    def __init__(self, spell, thing):
        self.spell = spell 
        self.thing = thing

    def lumus(self):
        print "You cast the spell %s with your %s wand at %s" %(self.spell, self.wandtype, self.thing)   #This line causes the AttributeError! 
        print "The room lights up."

    def wingardium_leviosa(self): 
        print "You cast the levitation spell."

my_wand = Wand('Phoenix-feather', '12 inches') 
cast_spell = CastSpell('lumus', 'door') 
my_wand.fulldesc()  
cast_spell.lumus() 

我尝试使用 super() 方法但无济于事。我非常感谢您帮助理解 a) 为什么类继承在这种情况下不起作用,b) 如何让它工作。


简单来说,你覆盖Wand.__init__在继承它的类中,所以CastSpell.wandtype从未设置过CastSpell。除此之外,my_wand无法将信息传递到cast_spell,所以你对继承的作用感到困惑。

不管你怎么做,你都必须以某种方式通过length and wandtype to CastSpell。一种方法是将它们直接包含到CastSpell.__init__:

class CastSpell(Wand):
    def __init__(self, spell, thing, length, wandtype):
        self.spell = spell 
        self.thing = thing
        self.length = length
        self.wandtype = wandtype

另一种更通用的方法是将这两个传递给基类自己的__init__():

class CastSpell(Wand):
    def __init__(self, spell, thing, length, wandtype):
        self.spell = spell 
        self.thing = thing
        super(CastSpell, self).__init__(length, wandtype)

另一种方法是停止制作CastSpell继承自Wand (is CastSpell一种Wand?或某事Wand吗?),而是让每根魔杖都能够拥有一些CastSpell其中 s:而不是“is-a”(aCastSpell是一种Wand),尝试“has-a”(aWand has Spells).

这是一个简单但不太好用的魔杖存储咒语的方法:

class Wand(object):
    def __init__(self, wandtype, length):
        self.length = length
        self.wandtype = wandtype
        self.spells = {} # Our container for spells. 
        # You can add directly too: my_wand.spells['accio'] = Spell("aguamenti", "fire")

    def fulldesc(self):
        print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)

    def addspell(self, spell):
        self.spells[spell.name] = spell

    def cast(self, spellname):
        """Check if requested spell exists, then call its "cast" method if it does."""
        if spellname in self.spells: # Check existence by name
            spell = self.spells[spellname] # Retrieve spell that was added before, name it "spell"
            spell.cast(self.wandtype) # Call that spell's cast method, passing wandtype as argument
        else:
            print "This wand doesn't have the %s spell." % spellname
            print "Available spells:"
            print "\n".join(sorted(self.spells.keys()))


class Spell(object):
    def __init__(self, name, target):
        self.name = name
        self.target = target

    def cast(self, wandtype=""):
        print "You cast the spell %s with your %s wand at %s." % (
               self.name, wandtype, self.target)
        if self.name == "lumus":
            print "The room lights up."
        elif self.name == "wingardium leviosa":
            print "You cast the levitation spell.",
            print "The %s starts to float!" % self.target

    def __repr__(self):
        return self.name

my_wand = Wand('Phoenix-feather', '12 inches')
lumus = Spell('lumus', 'door')
wingardium = Spell("wingardium leviosa", "enemy")

my_wand.fulldesc()
lumus.cast() # Not from a Wand! I.e., we're calling Spell.cast directly
print "\n\n"

my_wand.addspell(lumus) # Same as my_wand.spells["lumus"] = lumus
my_wand.addspell(wingardium)
print "\n\n"

my_wand.cast("lumus") # Same as my_wand.spells["lumus"].cast(my_wand.wandtype)
print "\n\n"
my_wand.cast("wingardium leviosa")
print "\n\n"
my_wand.cast("avada kadavra") # The check in Wand.cast fails, print spell list instead
print "\n\n"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 类继承属性错误 - 为什么?怎么修? 的相关文章

随机推荐

  • 如何创建 Java 自定义 Web 控件?

    这个问题最初出现在我的脑海中 我可以在 Servlet 中使用 AWT 控件吗 这将显示我对这个问题的无知 我是 JAVA 技术的新手 但经过一些阅读后 我似乎了解 AWT 控件直接连接操作系统 GUI 元素 因此无法在 Servlet 中
  • 对齐 CSS 网格中的列

    我应该设置哪些 CSS 属性 以便各列相互缠绕 忽略水平相邻列的高度 失败的尝试 我正在尝试这样做display grid 但它的行为不符合我的要求 一个例子 header height 2 0rem background PeachPuf
  • 通过 PowerShell/cmd 调用 Linux 应用程序的 Windows 子系统 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 随着最近推出的 Windows 内部版本 14316 我们拥有了适用于 Linux 的 Windows 子系统 测试版 它允许在 Wind
  • 设置超时时 JavaScript 闭环问题

    我在教程中找到了一些示例 说这是规范的示例 for var i 1 i lt 5 i setTimeout function console log i i i 1000 现在 我明白了 闭包将当前范围传递给函数 我假设它应该输出 1 2
  • AWS ELB - 多VPC负载均衡

    我正在开发一个项目 其中任务之一是为远程 VPC 中的实例提供 AWS ELB 服务 AWS ELB 位于 VPC A 中 实例位于 VPC B 中 我最初的回答是 否 因为 ELB 是负载均衡器 而不是路由器 文档和使用 AWS ELB
  • 如何在不打开网络浏览器的情况下在spotipy中进行身份验证?

    我尝试了标准方法 主页上有描述 但我需要在没有网络浏览器的情况下登录 我怎样才能做到这一点 您可以使用 Spotify 的 python 库之一 例如 https github com plamere spotipy https githu
  • setMicrophoneMute() 如何工作?

    我一直在尝试使用Android的AudioManager setMicrophoneMute 没有取得多大成功 也就是说 无论我做什么 它都拒绝将麦克风静音 我在网上搜索了一些线索 发现了一些报告类似经历的参考资料 AudioManger
  • 使用 CA 签署的另一个证书签署证书

    是否可以使用由 CA 签名的证书作为其他证书的 CA 来签署新证书 并且仍然由根 CA 验证它们 例子 create new key openssl genrsa des3 out server key 2048 openssl req n
  • 如何将文件复制到android的文件系统?

    如何将文件复制到android中的文件系统 位置 我怎样才能访问它 将文件从android复制到本地 adb pull data data com example sample sample 从本地复制文件到android adb push
  • 对常量值使用 def 与 val 有何含义

    使用有什么影响def vs val在 Scala 中定义一个常量 不可变的值 我显然可以写出以下内容 val x 3 def y 4 var a x y 7 这两种说法有什么区别 哪一种表现更好 是推荐的方式 更惯用 我什么时候会使用其中一
  • 如何给Tomcat添加健康检查?

    我想在 Linux 的 Tomcat 上配置一个简单的健康检查 例如 http localhost 8080 health http localhost 8080 health 我一直在检查Tomcat文档 我发现了这个内置的服务器状态站点
  • 如何安装最新版本的 TensorFlow 2?

    我想知道为什么我无法安装TensorFlow 2 0 0 到目前为止稳定版本在他们的官方网站上 https www tensorflow org versions even in PyPi https pypi org project te
  • python 中的弱引用列表

    我需要一个弱引用列表 当它们消失时会删除它们 目前 我执行此操作的唯一方法是不断刷新列表 手动删除死引用 我知道有一个 WeakKeyDictionary 和一个 WeakValueDictionary 但我真的想要一个 WeakList
  • 构造失败通知:非法构造函数

    我的网站使用从未在移动设备上运行过的桌面通知 但我最近开始在 Android 4 4 上的 Chrome 版本 42 0 2311 108 中收到以下异常 Failed to construct Notification Illegal c
  • 如何在页面 API 中获取图像 url 或下载图像的 url,其中图像是由流场创建的?

    在我的 wagtail 应用程序中 我有一个流字段 用于使用 ImageChooserBlock 上传图像以及标题和文本 这意味着在单个流字段中我有一个标题 一个文本和一个图像上传输入 我正在尝试在其余框架的页面 API 中获取图像 url
  • 将 MomentJS 与 TypeScript 结合使用 - moment() 有什么类型?

    我目前正在将我的项目从 ES5 转换为 ES6 但我遇到了 MomentJS 的问题 version 2 18 1 问题是我有一些变量是 Moment 对象 但我无法对它们调用 moment 一个例子 import as moment fr
  • 使用 javascript 捕获桌面屏幕截图

    是否可以使用javascript捕获客户端Windows桌面的屏幕截图 那么在linux和mac操作系统中呢 澄清 这个问题有not被问到或回答 因为我希望捕获整个桌面 而不仅仅是浏览器 我还想捕获 本机 屏幕截图 而不是从某些底层表示重新
  • 为什么我想在一个函数中锁定两个互斥体 - 这也带有延迟锁?

    https en cppreference com w cpp thread lock tag https en cppreference com w cpp thread lock tag void transfer bank accou
  • Grails UrlMapping 重定向以保持 DRY

    我正在与Grails 2 1 1并希望添加一些映射到控制器操作的自定义 URL 我可以做到这一点 但原始映射仍然有效 例如 我创建了一个映射add property to directory in my UrlMappings如下 clas
  • Python 类继承属性错误 - 为什么?怎么修?

    SO 上的类似问题包括 this one https stackoverflow com questions 10268603 python class inheritance attributeerror subclass object