在下面的方法定义,什么是*和**为做param2?
def foo(param1, *param2):
def bar(param1, **param2):
Run Code Online (Sandbox Code Playgroud) python syntax parameter-passing variadic-functions argument-unpacking
*运算符在Python 中的含义是什么,例如在代码中zip(*x)或f(**k)?
python syntax parameter-passing argument-unpacking iterable-unpacking
有什么区别:
class Child(SomeBaseClass):
def __init__(self):
super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)
和:
class Child(SomeBaseClass):
def __init__(self):
SomeBaseClass.__init__(self)
Run Code Online (Sandbox Code Playgroud)
我已经看到super在只有单继承的类中使用了很多.我可以看到为什么你在多重继承中使用它,但不清楚在这种情况下使用它的优点是什么.
我想给一个子类一些额外的属性,而不必显式调用一个新方法.那么有没有一种方法可以为继承的类提供一个__init__不会覆盖__init__父类方法的类型方法?
我编写下面的代码纯粹是为了说明我的问题(因此属性的命名很差等).
class initialclass():
def __init__(self):
self.attr1 = 'one'
self.attr2 = 'two'
class inheritedclass(initialclass):
def __new__(self):
self.attr3 = 'three'
def somemethod(self):
print 'the method'
a = inheritedclass()
for each in a.__dict__:
print each
#I would like the output to be:
attr1
attr2
attr3
Run Code Online (Sandbox Code Playgroud)
谢谢
关于SO的类似问题包括:这个和这个.我还阅读了我能找到的所有在线文档,但我仍然很困惑.我很感激你的帮助.
我想在CastSpell类lumus方法中使用Wand类.wandtype属性.但我不断收到错误"AttributeError:'CastSpell'对象没有属性'wandtype'."
此代码有效:
class Wand(object):
def __init__(self, wandtype, length):
self.length = length
self.wandtype = wandtype
def fulldesc(self):
print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
class CastSpell(object):
def __init__(self, spell, thing):
self.spell = spell
self.thing = thing
def lumus(self):
print "You cast the spell %s with your wand at %s" %(self.spell, self.thing)
def wingardium_leviosa(self):
print "You cast the levitation spell."
my_wand = Wand('Phoenix-feather', '12 inches')
cast_spell = CastSpell('lumus', 'door') …Run Code Online (Sandbox Code Playgroud)