- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下类(class)
class Sample(object):
def __init__(self, argument, argument2, argument3):
self.value = argument
self.value2 = argument2
self.value3 = argument3
我想使用 type 创建它的子类,但是我不确定如何填充 __ init __ 方法的参数。
我还有这个自定义的 __ init __ 方法来填充对象:
def setup(self, arg, arg2, arg3):
self.value = "good"
self.value2 = "day"
self.value3 = "sir"
myclass = type("TestSample", (Sample,), dict(__init__=setup))
但是当我执行时:
myclass()
我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: setup() takes exactly 4 arguments (1 given)
有没有办法预先填充这些值,而不必在对象实例化时提供它们?
最佳答案
您的子类工作正常,但您给了它自己的 __init__
方法,该方法仍然需要四个位置参数。其中之一是 self
,但在创建对象时您仍然需要提供其他 3 个:
myclass('some', 'argument', 'values')
您的函数忽略这些参数,所以也许您不想将它们包含在函数签名中?您不必在这里匹配父类:
def setup(self):
self.value = "good"
self.value2 = "day"
self.value3 = "sir"
myclass = type("TestSample", (Sample,), dict(__init__=setup))
您仍然可以将其委托(delegate)给父类,而不是直接设置属性:
def setup(self):
Sample.__init__(self, 'good', 'day', 'sir')
myclass = type("TestSample", (Sample,), dict(__init__=setup))
如果您希望将这些设置为可以覆盖的默认,请使用关键字参数:
def setup(self, argument='good', argument2='day', argument3='sir'):
Sample.__init__(self, argument, argument2, argument3)
myclass = type("TestSample", (Sample,), dict(__init__=setup))
现在您可以省略参数,或为它们提供不同的值:
c1 = myclass()
c2 = myclass(argument2='weekend')
关于Python27 : Subclassing a class by using type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30322161/
我想知道在从另一个扩展的类中创建对象的这两种方法之间是否存在任何功能差异。 我只是在处理基本的继承,除了 Cat extends Mammal 之类的东西之外没有什么复杂的。 最佳答案 ParentC
class superClass {} class subClass extends superClass{} public class test { public static void m
我有一个跨很多很多项目的 C# 解决方案。我创建了一个从 Systme.Diagnostics.Tracing.EventSource 继承的跟踪类,称为 MyCustomEventSource,它处
考虑以下示例,其中 Student 继承自 Person: function Person(name) { this.name = name; } Person.prototype.say =
基本上是标题所说的内容,但有一些详细说明。我有一个带有几个子类的父类(super class)。我需要一个 ArrayList 来保存两种类型的子类,因此需要 SuperClass 类型的 Array
我正在阅读 mughal 的 SCJP(第 3 版)(我遇到的最好的 SCJP 书),在第 727 页上,它说了以下内容: class MyIntList extends ArrayList {}
我使用方法 Object process( JobContext jobContext ); 以及名为 JobProcessImpl 的 impl 来定义 JobProcess。每当执行此 JobPr
如果我想编写一个采用可变数量“TDerived”的方法,其中 TDerived 是类“Base”的任何子类,有什么办法可以做到这一点吗? 以下代码仅适用于单个特定的指定子类: void doStuff
有没有人(本地)弄清楚如何对 SKNode 进行子类化以包含通过关节连接的多个物体 - 例如汽车? 似乎没有办法将子类的关节添加到父场景的物理世界属性中。 此外,当尝试编译并运行下面的对象时,即使没有
我正在尝试编写 Node 库的扩展。该库导出一个扩展 EventEmitter 的类(class LibClass extends EventEmitter)。它负责建立一个 websocket 连接
Tensorflow 有一些 docs用于子类化 (tf) Keras Model 和 Layer。 但是,尚不清楚使用哪个“模块”或“ block ”(例如,几个层的集合)。 由于从技术上讲它是几个
我正在尝试对 android.os.AsyncTask 类进行通用子类化。我基本上只想给它添加一个属性。问题是,我仍然希望能够将它用作匿名类。 import android.content.C
是否可以在 swift 中对通用结构进行子类化? 假设我们有一个结构: struct Foo {} 我想要“子类化”它以添加一些功能: struct Something {} struct Bar
class A: pass class B(A): pass ListOfA = List[A] list_of_a : ListOfA = [A(), A()] for e in [
我想通过打印选择器和参数来拦截发送到代理对象的消息。即使代理没有实现它们并且没有目标对象。请帮忙。我已经查看了几个选项和 Apple 文档,但他们假设您已经知道目标对象。我想干净地完成此操作,而不会出
我有一个使用核心数据并有两个目标的项目。这些目标有几个共享实体。目前,我正在维护两个具有重复实体的独立模型,在我看来,这效率不高。 有没有办法将共享数据模型作为具体目标中数据模型的父级? 最佳答案 我
我收到以下错误: Could not find matching constructor for: org.crawler.CrawlerUtils$fetch(org.series.crawler.
在 Knockout 中,我有一个 Setting 实体数组,它们可能具有不同的类型(即设置实体的“子类”)。这些类型是动态的,用户可以在 UI 中更改它们。根据设置实体的类型,它需要不同的对象属性,
我有一个ArrayClass并且mergeSortArray扩展了它。并且 mergeSortArray 包含一个 mergeSort() 方法。但是,由于我使用 super 从父类(super cl
我是 PyTorch 的新手,在使用不同的工具包一段时间后尝试它。 我想了解如何对自定义层和函数进行编程。作为一个简单的测试,我写了这个: class Testme(nn.Module):
我是一名优秀的程序员,十分优秀!