python - Codacy bad 给 super 的第一个论点

标签 python optimization codacy

在通过 codacy 审查一些代码时,Codacy 给出了以下代码段的问题:

def MyClass(OldClass):
    def __init__(self, arg1, arg2, *args, **kwargs)
        self.arg1 = arg1
        self.arg2 = arg2
        super(OldClass, self).__init__(*args, **kwargs)

附上以下解释:

Why is this an issue?

For example, calling super() with the base class as first argument is wrong:

class AnotherOldStyleClass(OldStyleClass):
    def __init__(self):
        super(OldStyleClass, self).__init__() The super invocation 

should be:

super(AnotherOldStyleClass, self).__init__()


似乎要我这样做:
def MyClass(OldClass):
    def __init__(self, arg1, arg2, *args, **kwargs)
        self.arg1 = arg1
        self.arg2 = arg2
        super(OldClass, self).__init__(*args, **kwargs)

或者这个:
def MyClass(OldClass):
    def __init__(self, arg1, arg2, *args, **kwargs)
        super(MyClass, self).__init__(*args, **kwargs)
        self.arg1 = arg1
        self.arg2 = arg2

有人能告诉我哪些是正确的,为什么这是首选行为?

供引用,here例如,我发现使用选项 2。

编辑:这是我的代码,正如它显示的那样。这解释了我的错误:
class TransferToBigQuery(GoogleCloudStorageToBigQueryOperator): 
    """Class to transfer data from Google cloud storage to Big Query""" 
    def __init__( 
        self, id, bucket, destination_project_dataset_table, source_objects=None, 
        schema_fields=None, schema_object=None, source_format='CSV', 
        create_disposition='CREATE_IF_NEEDED', 
        skip_leading_rows=0, write_disposition='WRITE_EMPTY', 
        field_delimiter=',', max_id_key=None, file_xcom=None, 
        bigquery_conn_id='bigquery_default', 
        google_cloud_storage_conn_id='google_cloud_storage_default', 
        delegate_to=None, schema_update_options=(), *args, **kwargs): 
        super(GoogleCloudStorageToBigQueryOperator, self).__init__(*args, 
                                                               **kwargs) 

为什么推荐这个?

最佳答案

我希望下一个例子能解释不同之处

class Grandparent(object):
    def hello(self):
        print "hi, I am Grandparent"

class Parent(Grandparent):
    def hello(self):
        print "hi, I am Parent"

class Child(Parent):
    def test(self):
        super(Parent, self).hello()   # print "hi, I am Grandparent"
        super(Child, self).hello()  # print "hi, I am Parent"

    def proper_way_to_do_same(self):
        Grandparent.hello(self)   # print "hi, I am Grandparent"
        Parent.hello(self)  # print "hi, I am Parent"

关于python - Codacy bad 给 super 的第一个论点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580179/

相关文章:

python - 将列组与 bool 值组合以创建多个新列

mysql - 如果结果不够,如何动态添加 SELECT 语句?

python - 在Python中将字典分配给对象的有效方法

Meteor-coverage 似乎将执行的语句显示为未覆盖

javascript - github 中的 Codacy 功能给了我一些我无法理解的错误

python - cmap 到 Matplotlib 中的 rgba

Python - Python 中来自 R 的函数 "which"

python - 我想分割一个节点的文本,然后将它们放入独立的元素中

c++ - 我如何在 std::atomic<T> 上实现一个简单的自旋锁,以便编译器不会优化它?