5

While reviewing some code through codacy, Codacy gave an issue for the following piece of code:

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

With the following explanation:

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__()

It seems to want me to do this:

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

Or perhaps this:

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

Can someone tell me which of these is correct and why this is the preferred behaviour?

For reference, here is as example that I found using option 2.

Edit: here is my code as it appears exactly. Which explains my error:

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) 

Why is this recommended?

Daniel Lee
  • 7,189
  • 2
  • 26
  • 44
  • No, the first alternative is the *opposite* of what it's telling you to do. However, there's nothing wrong with the original code as posted: are you sure that's what you actually used? – Daniel Roseman Oct 05 '17 at 07:33
  • Both seems OK. There is a difference if you pass the call to super as first or last statement in your init. – Chen A. Oct 05 '17 at 07:38
  • Thanks @DanielRoseman, I was in fact and had the wrong argument in super. Can someone please tell me why this is preferred? – Daniel Lee Oct 05 '17 at 07:42

1 Answers1

3

I hope next example will explain the difference

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"
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88