相关疑难解决方法(0)

'super().__init__()' 在 python 3.x 中是什么意思?

这两个代码示例有什么区别?

1:

class SubType(type):
    def __init__(cls, name, bases, dct):
        super().__init__(name, bases, dct)
Run Code Online (Sandbox Code Playgroud)

2:

class SubType(type):
    def __init__(cls, name, bases, dct):
        pass
Run Code Online (Sandbox Code Playgroud)

metaclass class super python-3.x

6
推荐指数
2
解决办法
2万
查看次数

Django 导入 - 导出:尝试在具有唯一或唯一性_together 约束的字段中插入重复记录时出现 IntegrittyError

更新

我已提交功能请求。我们的想法是passIntegrittyError的时候由数据库产生uniqueunique_together拒绝已经存在于数据库中的记录。


我有以下模型:

class Compositions(models.Model):
    composer_key = models.ForeignKey(
        Composer,
        )
    composition = models.CharField(
        max_length=383,
        )

    class Meta(object):
        unique_together = (('composer_key', 'composition'), )
Run Code Online (Sandbox Code Playgroud)

在管理界面中使用 django-import-export,而不为idcsv 文件中的每个条目提供一个,...如果一对 csv 文件已经存在,该过程将因完整性错误而中断

duplicate key value violates unique constraint "data_compositions_composer_key_id_12f91ce7dbac16bf_uniq"
DETAIL:  Key (composer_key_id, composition)=(2, Star Wars) already exists.
Run Code Online (Sandbox Code Playgroud)

CSV 文件如下:

id  composer_key    composition
        1           Hot Stuff
        2           Star Wars
Run Code Online (Sandbox Code Playgroud)

这个想法skip_row在管理员中使用和实现它。

管理.py:

class CompositionsResource(resources.ModelResource):

    class Meta:
        model = Compositions
        skip_unchanged …
Run Code Online (Sandbox Code Playgroud)

python django django-import-export

6
推荐指数
2
解决办法
3228
查看次数

为什么python super不接受只有实例?

在python 2.x中,super接受以下情况

class super(object)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)
 |  Typical use to call a cooperative superclass method:
Run Code Online (Sandbox Code Playgroud)

据我所知,super是一个类,包装类型和(最终)实例来解析类的超类.

我对以下几件事感到困惑:

  • 为什么也没有super(instance),具有典型的用法,例如super(self).__init__().从技术上讲,您可以从对象本身获取对象的类型,因此当前策略super(ClassType, self).__init__()是多余的.我假设与旧式类或多重继承的兼容性问题,但我想听听你的观点.
  • 另一方面,为什么python 3会接受(请参阅使用__init __()方法了解Python super())super().__init__()?我在这看到了一种魔法,违背了显性比隐含禅更好.我会看到更合适的 self.super().__init__().

python language-design

5
推荐指数
1
解决办法
632
查看次数

super(ClassName,self)有什么用._ init_()

我有一个看起来像这样的课程:

#!/usr/bin/env python
class Foo:
    def __init__(self, x):
        self.x = x
    def bar(self):
        self.bar1_out = self.x + 5
        self.bar2_out = self.x + 1
        return (self.bar1_out,self.bar2_out)
    def qux(self,myvalue = None):
        first, second = myvalue or self.bar()
        return first + 3, second + 6

def main():
    """docstring for main"""
    f = Foo(5)

    mbr_out1, mbr_out2 = f.bar()
    print mbr_out1, "\t", mbr_out2

    mqx_out1, mqx_out2 = f.qux()
    print mqx_out1, "\t", mqx_out2

    qout1, qout2 = f.qux((1))
    print qout1, "\t", qout2

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

我看到了一些建议使用的实现 …

python oop class

5
推荐指数
1
解决办法
3036
查看次数

关闭PyQt窗口后如何执行更多代码?

这是一个例子如下:

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
    print "you just closed the pyqt window!!! you are awesome!!!"
Run Code Online (Sandbox Code Playgroud)

当窗口打开或关闭窗口后,上面的print语句似乎没有执行.我想在关闭窗口后进行打印.

python pyqt

5
推荐指数
1
解决办法
3428
查看次数

在父类中调用重写的父类方法

我正在尝试覆盖两个父类函数trainevalChildClass. 在父类中,eval()基本上调用train(). 但是,我知道,当我写我的代码如下,eval()父类试图调用该函数train()ChildClass-我想eval()在父类调用train()父类代替。

我只是想知道是否有任何干净的解决方案来进行更改以ChildClass允许父类调用父train()函数?

class ChildClass(nn.Module):
    def __init__(self):
        super(ChildClass, self).__init__()

    def train(self):
        super(ChildClass, self).train()

    def eval(self):
        super(ChildClass, self).eval()
Run Code Online (Sandbox Code Playgroud)

父类位于 Python 包 ( pytorch) 中,因此不应进行任何更改:

class Module(object):
    #...

    def train(self, mode=True):
        # ...
        return self

    def eval(self):
        return self.train(False)
Run Code Online (Sandbox Code Playgroud)

python python-3.x pytorch

5
推荐指数
1
解决办法
309
查看次数

[python]:被super()搞糊涂了

可能重复:
了解Python super()

类的B子类A,所以在B中__init__我们应该__init__像这样调用A :

class B(A):
    def __init__(self):
        A.__init__(self)  
Run Code Online (Sandbox Code Playgroud)

但是super(),我看到这样的事情:

class B(A):
    def __init__(self):
        super(B, self).__init__()  #or super().__init__()
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 为什么不super(B, self).__init__(self)呢?仅仅因为返回代理对象是绑定的?

  2. 如果我在super中省略第二个参数并且返回代理对象是未绑定的,那么我应该写super(B).__init__(self)吗?

python super

4
推荐指数
1
解决办法
1246
查看次数

在Python中调用构造函数的顺序

#!/usr/bin/python

class Parent(object):        # define parent class
   parentAttr = 100
   def __init__(self):
      print "Calling parent constructor"

   def parentMethod(self):
      print 'Calling parent method'

   def setAttr(self, attr):
      Parent.parentAttr = attr

   def getAttr(self):
      print "Parent attribute :", Parent.parentAttr


class Child(Parent): # define child class
   def __init__(self):
      print "Calling child constructor"

   def childMethod(self):
      print 'Calling child method'


c = Child()          # instance of child
Run Code Online (Sandbox Code Playgroud)

我调用了这里创建了一个Child类的实例.它似乎没有调用父类的构造函数.输出如下所示.

Calling child constructor
Run Code Online (Sandbox Code Playgroud)

例如,在C++中调用派生类的构造函数时,首先调用基类构造函数.为什么在Python中不会发生这种情况?

python

4
推荐指数
2
解决办法
4280
查看次数

什么是python中的超级(类型)?

班级定义:

class A(object):
    def foo(self):
        print "A" 


class B(object):
    def foo(self):
        print "B" 


class C(A, B): 
    def foo(self):
        print "C"
Run Code Online (Sandbox Code Playgroud)

输出:

>>> super(C)
<super: <class 'C'>, NULL>
>>> super(C).foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'super' object has no attribute 'foo'
Run Code Online (Sandbox Code Playgroud)

如果我们无法访问类的属性,那么super(type)有什么用?

python inheritance super

4
推荐指数
1
解决办法
1717
查看次数

在蟒蛇中揭开神秘的神秘面纱?

我试图了解python中的超级工作方式,并尝试了以下示例:

class A(object):
    def __init__(self):
        print "in A's init"

class B(object):
    def __init__(self):
        print "in B's init"

class C(A,B):
    def __init__(self):
        super(C,self).__init__()
        print "In C"

if __name__=="__main__":
    c=C()
Run Code Online (Sandbox Code Playgroud)

相当简单..我尝试了以下超级调用(在此处显示结果):

>>> super(B,c).__init__()
>>> super(B,c).__init__()
>>> super(A,c).__init__()
    in B's init
>>> super(A,c).__init__()
    in B's init
>>> super(A,c).__init__()
    in B's init
>>> super(B,c).__init__()
>>> super(C,c).__init__()
    in A's init
Run Code Online (Sandbox Code Playgroud)

我不明白为什么super(A,c).__init__()在B的init 中打印出它?

python super

4
推荐指数
1
解决办法
197
查看次数