Python中文
首页
教程
问答
标签
搜索
登录
注册
无法从超类继承
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有下面这样一个简单的代码,类Employee应该从类Person继承。你知道吗</p> <pre><code>class Person: def __init__(self, firstname, lastname, age): self.firstname = firstname self.lastname = lastname self.age = age def getname(self): return self.firstname, self.lastname def getage(self): return self.age class Employee(Person): def __init__(self, first, last, empid, age): Person.__init__(self, first, last, age): self.empid = empid def getemp(self): return self.getname() + ", " + self.empid employee = Employee("Bart", "Simpson", 1006, 16) print(employee.getemp()) </code></pre> <p>它给了我这个错误:</p> <pre><code>File "/tmp/pyadv.py", line 156 Person.__init__(self, first, last, age): ^ SyntaxError: invalid syntax </code></pre> <p>我检查了Python<a href="https://docs.python.org/2/tutorial/classes.html" rel="nofollow noreferrer">documentation</a>关于类的内容,但它在子类的<code>__init__()</code>中没有超类初始化。但我发现在其他网站,如<a href="http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/" rel="nofollow noreferrer">here</a>,狗继承宠物。 那么,我错过了什么?你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>改变</p> <p><code>Person.__init__(self, first, last, age):</code>到</p> <p><code>super().__init__(first, last, age)</code></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
当pandas中出现小数据时,DataFrame.resample()被调用时,内存不足
1 回答
当Pandas中存在许多条件时,如何过滤数据?
9 回答
当pandas中没有公共列名时,我们应该如何组合多个数据集而不在行或列中重复
2 回答
当pandas中的值为null时,不使用pandas的to_列表
5 回答
当Pandas中的值和条目匹配时,如何计算键的计数?
2 回答
当pandas列中有字符串时,如何从该列获取最长长度的string/integer/float
7 回答
当pandas列重命名创建列名重复时该怎么办
10 回答
当Pandas在带有颜色标签的多个子图中创建散点图时,无法设置xlabel
3 回答
当pandas导入openpyxl模块时,该模块没有属性''uuuversion''
8 回答
当pandas数据在python中聚合时,如何计算每组尾部的sum | mean |中值
8 回答
当Pandas数据头以列(如xls)拆分时,如何读取Pandas数据头
8 回答
当Pandas数据帧包含nan时,用“新信息”更新Pandas数据帧
8 回答
当pandas是imp时,Cx\u freeze TypeError只能使用numpy依赖项将list(而不是“NoneType”)连接到list
6 回答
当Pandas满足一定条件时,如何放弃整个群体
8 回答
当Pandas的时间增量指数不规则时,如何获得时间序列值的日差?
9 回答
当Pandas通过read\u csv方法读取大文件时。IOError:[Errno 13]权限被拒绝
9 回答
当panda列有空值时,如何将其转换为int?
7 回答
当param=''时使用Beautiful Soup访问属性的参数
6 回答
当paramiko抛出“无现有会话”异常时,标准输入流挂起
3 回答
当parentchild在同一端口上监听时会发生什么?
5 回答