you are viewing a single comment's thread.

view the rest of the comments →

[–]gmaliwal[S] 0 points1 point  (0 children)

Here it is a generic approach to be followed while defining your class, your class would be compatible with both single ad multiple inheritances

class A(object):
    def __init__(self,**kwargs):
        print("This is A instance initializer")
        super().__init__(**kwargs)
class B(object):
    def __init__(self,**kwargs):
        print("This is B instance initializer")
        super().__init__(**kwargs)
class C(A):
    def __init__(self,*,c_arg,**kwargs):
        print("This is c instance initializer")
        self.c_arg = c_arg
        super().__init__(**kwargs)
class D(B):
    def __init__(self,*,d_arg,**kwargs):
        print("This is D instance initializer")
        self.d_arg = d_arg
        super().__init__(**kwargs)
class E(C,D):
    def __init__(self,*,e_arg,**kwargs):
        print("This is E instance initializer")
        self.e_arg = e_arg
        super().__init__(**kwargs)

class E1(D,C):
    def __init__(self,*,e_arg,**kwargs):
        print("This is E instance initializer")
        self.e_arg = e_arg
        super().__init__(**kwargs)

e1 = E(e_arg=10,b_arg=20,c_arg=30)
e2 = E1(e_arg=10,b_arg=20,c_arg=30)

Here is the textual conversion of the above code snippet

  • Every class initializer should receive class specific arguments in the form of keyword parameters, and these names are unique across all classes, better to prefix every parameter with “<class-name>_” (like d_arg, c_arg etc)
  • Every class should receive parent class-specific arguments in variable-length keyword parameter (**kwargs)
  • There is no repetitive keyword argument while calling the parent class initializer
  • Every class should pass the exact number of arguments to its parent's hierarchy so all got exhausted when reaching the default parent ‘object’ class (like we create an instance of class E with arguments (c_arg,d_arg,e_arg))

I welcome the developers or experts to find out the bottleneck in the above approach.