相关疑难解决方法(0)

2149
推荐指数
17
解决办法
65万
查看次数

星级算子是什么意思?

*运算符在Python 中的含义是什么,例如在代码中zip(*x)f(**k)

  1. 如何在解释器内部处理?
  2. 它会影响性能吗?是快还是慢?
  3. 什么时候有用,什么时候不用?
  4. 它应该用于功能声明还是通话中?

python syntax parameter-passing argument-unpacking iterable-unpacking

559
推荐指数
5
解决办法
17万
查看次数

"超级"在Python中做了什么?

有什么区别:

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在只有单继承的类中使用了很多.我可以看到为什么你在多重继承中使用它,但不清楚在这种情况下使用它的优点是什么.

python oop inheritance super

522
推荐指数
7
解决办法
21万
查看次数

继承类中的python __init__方法

我想给一个子类一些额外的属性,而不必显式调用一个新方法.那么有没有一种方法可以为继承的类提供一个__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)

谢谢

python oop inheritance

19
推荐指数
3
解决办法
3万
查看次数

Python类继承AttributeError - 为什么?怎么修?

关于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)

python inheritance class

6
推荐指数
1
解决办法
6057
查看次数