gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:59:41 24 4
gpt4 key购买 nike

在通过 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com