python - 获取子类的属性

标签 python attributes

如何获取子类的属性? 我的意思是age=age

部分
def method(name='thomas', age=27):
   submethod(age=age)

最佳答案

我不太确定这段代码的意思,子方法是抽象的吗?

无论如何,你缺少self,那不是一个方法。这应该有效:

def method(self, name='Thomas', age=27):
  self.submethod(name, age)

如果子方法参数只是关键字,为了清楚起见,您可以重命名本地参数值:

def method(self, name='Thomas', age=27):
  the_age = age
  self.submethod(name, age=the_age)

或者只是不这样做,关键字名称无论如何都不会与局部变量冲突,因此一旦添加 self,您的原始代码就可以工作:

def method(self, name='Thomas', age=27):
  self.submethod(name, age=age)

关于python - 获取子类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4780785/

相关文章:

python - 是否可以在 Sublime Text 3 输出面板中显示图像?

python - 使用 Twisted 子进程和 virtualenvwrapper 时如何导入模块?

java - 如何解决servlet类和jsp.file之间传递属性时出现空指针异常

jquery - 选择值列表中具有属性值的元素

asp.net-mvc - 是否有任何与 AJAX 相关的属性可以为 ASP.NET MVC Controller 操作设置?

jquery - 使用 jQuery 单击后更改链接 href

Python pandas dataframe 转换不带小数的值

python - 如何在命令行上处理 utf8(使用 Perl 或 Python)?

python - Google OpenID 和 OAuth - 如何获取访问 token

python - 如何使 Python/Sphinx 文档对象属性仅在 __init__ 中声明?