python - 如何删除子类中的继承函数?
问题描述
注意:它在python中。简化的代码是,
class main:
def __init__(self):
pass
def unneededfunction(self):
print("unneeded thing")
class notmain(main):
def __init__(self):
pass
#code for getting rid of unneededfunction here
问题是,你如何做到让 notmain.unneededfunction 不存在(即调用它会导致错误)?
解决方案
如果你不想notmain
拥有unneededfunction
thennotmain
不应该是main
. 以这种方式对抗系统,它打败了整个继承点。
如果您真的坚持这样做,notmain
可以重新定义unneededfunction
并引发如果unneededfunction
不存在时会引发的相同异常,AttributeError
. 但是再一次,你违背了粮食。
除此之外,您不能删除unneededfunction
from,notmain
因为notmain
它不拥有该方法,它的父类拥有main
。
推荐阅读
- python - 从数据框单元格中的字符串中删除单词/字符?
- sql-server - 为什么将列从 nvarchar 更改为 varchar 会导致 mdf 文件大幅增长?
- ruby-on-rails - 启动 puma w/o `bundle exec` 工作;不适用于捆绑执行;BUNDLE_APP_CONFIG env var 在 RUBYOPT="-rbundler/setup" 运行期间不可用
- mpi - 如何为 mpirun 配置主机文件和机器文件?
- java - Android Oreo 动画渲染问题
- php - 嵌套的 foreach 循环无法正常运行。- wp
- r - 如何在 RCpp 中向数据框添加新列?
- javascript - 正则表达式的字符类范围乱序
- sql - SQL 自加入日期
- python - 断言 pytest 命令已运行