Python Codacy给super的第一个错误参数
在通过codacy审查某些代码时,codacy对以下代码提出了一个问题:Python Codacy给super的第一个错误参数,python,optimization,codacy,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) 并作出以下解释: 为什么这是一个问题 例如,使用基类作为第一个
def MyClass(OldClass):
def __init__(self, arg1, arg2, *args, **kwargs)
self.arg1 = arg1
self.arg2 = arg2
super(OldClass, self).__init__(*args, **kwargs)
并作出以下解释:
为什么这是一个问题
例如,使用基类作为第一个参数调用super()是
错:
应该是:
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
有人能告诉我哪些是正确的,为什么这是首选行为
作为参考,这是我使用选项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"
不,第一种选择与它告诉你的相反。然而,发布的原始代码并没有什么问题:您确定这就是您实际使用的代码吗?两者似乎都可以。如果将调用作为init中的第一个或最后一个语句传递给super,则会有所不同。谢谢@DanielRoseman,事实上,我在super中的参数是错误的。有人能告诉我为什么这是首选吗?
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"