codacy在通过Codacy检查某些代码时,给出了以下代码的一个问题:
def MyClass(OldClass):
def __init__(self, arg1, arg2, *args, **kwargs)
self.arg1 = arg1
self.arg2 = arg2
super(OldClass, self).__init__(*args, **kwargs)有以下解释:
为什么这是个问题? 例如,使用基类作为第一个参数调用super()是错误的: 类AnotherOldStyleClass(OldStyleClass):def __init__(self):super(OldStyleClass,self).__init__() 应: 超级(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) 为什么推荐这样做?
发布于 2019-12-19 13:35:42
我希望下一个例子能解释其中的区别。
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"https://stackoverflow.com/questions/46580179
复制相似问题