- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有大量由大型库代码库的 import
定义的类,出于可维护性的原因,我不想修改它们。它们都继承自 BaseClass,而 BaseClass 包含一个我想扩充的方法。我认为以下是可行的解决方案
class MyMixin(object):
def method( self, args):
... # 1. a few lines of code copied from BaseClass's def of method
... # 2. some lines of my code that can't go before or after the copied code
... # 3. and the rest of the copied code
class MyAbcClass( MyMixin, AbcClass):
pass
# many similar lines
class MyZzzClass( MyMixin, ZzzClass):
pass
问题。有没有办法获取 ("MyXxxClass", XxxClass)
元组的列表,并编写定义 MyXxxClasses 的代码?是否足以理解它胜过上面的重复?
最佳答案
使用三个参数 type
定义类,然后将它们设置为 the module's global dictionary :
todefine = [('MyAbcClass', AbcClass), ...]
for name, base in todefine:
globals()[name] = type(name, (MyMixin, base), {})
如果要定义的名称遵循您提供的固定模式(`"My"+ 基类名称),您可以通过动态构造要定义的名称来更少地重复自己:
todefine = [AbcClass, ...]
for base in todefine:
name = "My" + base.__name__
globals()[name] = type(name, (MyMixin, base), {})
如果您试图包装给定模块中的所有类,您甚至可以通过内省(introspection)模块以编程方式生成 todefine
来避免显式列出类(如果您知道模块有或没有__all__
您可以只使用适当的方法,而不是尝试一种方法并默认使用另一种方法):
import inspect
try:
# For modules that define __all__, we want all exported classes
# even if they weren't originally defined in the module
todefine = filter(inspect.isclass, (getattr(somemodule, name) for name in somemodule.__all__))
except AttributeError:
# If __all__ not defined, heuristic approach; exclude private names
# defined with leading underscore, and objects that were imported from
# other modules (so if the module does from itertools import chain,
# we don't wrap chain)
todefine = (obj for name, obj in vars(somemodule).items() if not name.startswith('_') and inspect.isclass(obj) and inspect.getmodule(obj) is somemodule)
关于python - 在很多类 : DRY? 上定义相同的方法覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32631113/
我是 DRY principle 的坚定拥护者: Every piece of knowledge must have a single, unambiguous, authoritative rep
假设我有一个稍微复杂的 for 循环,用于不同的情况。有没有一种方法可以提取该 forloop 并仍然保持代码可读性? 例如: private function bar(){ for(i=0;
JSData 是替代还是补充 Sequelize.js? 我试图了解这两个库如何一起玩以及是否应该一起玩。 从我的阅读来看,JSData 主要处理访问数据,而 Sequelize.js 将创建和修改
我有 aws-eks 集群,下面是我替换现有配置的命令。 kubectl create configmap flink-config --from-file=./config -o yaml --dr
我正在设计一个可重用的类库,其中包含 2 个名为 core.xml.dll 和 core.string.dll 的程序集(其中包括)。 xml 程序集引用字符串程序集,以便使用一些字符串辅助方法。 但
我正在尝试找到一种更 DRY 的方式来使用 docker-compose env。 docker-compose-base.yml base: image: reactjs_web v
我有一些看起来像这样的类(class): struct A { void *stuff; int x; int foo() const; } 我有一些采用这种类型参数的函数,例如 int
我目前正在 Silverlight 中构建一些自定义控件。我希望这些控件能够响应验证错误。我想要做的是在我的控件周围设置红色边框,就像默认的 Silverlight 控件一样。 我的理解是我需要将它添
我正在处理一些服务器端代码,在将它们传递给客户端之前包装所有异常,因此所有面向客户端的方法都有以下代码 try{ DoSomething(); } catch (ExceptionA e) {
“纵深防御”原则指出,应该在多个地方实现约束,这样如果一条数据绕过或漏过一层,就会在下一层被捕获。一个很好的例子是在网络应用程序中——你将验证放在客户端 javascript 中,放在服务器端代码中(
使用这种方法在 MVC 中查看模型:http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view
我想将 jquery 脚本优化为多个元素(单个页面上有许多幻灯片)。我可以添加一些功能,但这并不干燥(不要重复自己)。 Pr
我有一些代码(不是我的),其中包含带有声明的范围映射的指令。我的愿望是在其他地方使用该指令并传递将在模板中使用的附加变量。 我要传递的变量是theVar。 我发现唯一可行的解决方案看起来很噩梦:
我将数据存储在数组中,并从该数组中使用循环创建表。对于一个表,我需要两列,另外 30 列(取决于数组项)。这将有三列。 var prodej = [ /*First column, second,
有没有更好的方法来声明来自同一组(例如“com.android.support”)具有相同版本(例如“23.4.0”)的多个软件包(例如“appcompat-v7”)? 实际申报: ... def a
我有一个看起来像这样的对象: var myObj = { _fooCon: function(f) { if (typeof f != 'function') throw
这就是我所拥有的。 Map data = new HashMap<>(); // assume this has been populated public int getLastestVersion
我在尝试维护 DRY 概念时遇到了 AngularJS 的问题 [不要重复自己]。我希望我做错了什么,有人可以指出我的错误。 一家公司销售卡车和汽车。 这两个项目具有相似和不同的属性。添加到其中一种的
我最终在几个类中得到了几个遵循相同模式的方法(每个类中总是有一堆这些方法): private void updateFoo() { String newFoo = fooTextField.g
假设我有一个字典config,其中包含键username和password。我想创建一个新字典,仅包含 config 中的username 和 password 键值对。一种方法是: new_dict
我是一名优秀的程序员,十分优秀!