-1

I tried to find out with googling, but it seems not explained.

There are four classes, for example, which are A, B, C, and D.

C is sub-class of D. A and B is calling C.

    Class A:
        def __init__(self):
            self.c = C()

    Class B:
        def __init__(self):
           self.c = C()

    Class C(D):
        def __init__(self):
            print('it is C')

    Class D:
        def __init__(self):
            print('it is D')


    a = A()
    b = b()

In this case, it will initiate twice for Class C. So, 'it is C' and 'it is D' strings represent twice. And.. I think it's ... not normal.

Unlike Java, it seems that Python doesn't support constructor overloading.

In this case, can I call C only though? Or, could I get an advice for using init in Python?

Thank you.

owcred601
  • 43
  • 7
  • What makes you think that you cant pass in additional parameters to `__init__`? It works the same way as java – Sayse Dec 02 '19 at 07:42
  • 1
    You create `C` twice in `A` and `B`, and `C` is extends `D`, so `D` constructor is also called. How is it different from Java? except of course you need to add `super().__init__()` to `C` `__init__`... – Guy Dec 02 '19 at 07:44
  • 1
    First, check indentation, is the indentation here correct? – Paritosh Singh Dec 02 '19 at 07:44
  • 1
    By the way what is *constructor overriding*? there is no such thing in Java – Guy Dec 02 '19 at 07:45
  • Oh, I just want to use the constructor in different way, so.. if I call C(1) then, it says 'it is C', but if I call C() without any argument, then it says nothing. – owcred601 Dec 02 '19 at 07:46
  • Your current code cannot even run in python, can you show the actual code you're running? – Paritosh Singh Dec 02 '19 at 07:46
  • I corrected your syntax and tried to run it which printed "it is C it is C" are you not looking for this output – Rikigami Dec 02 '19 at 07:49
  • 1
    Sorry for my English... It is Overloading. I found its usage with if-else statement. – owcred601 Dec 02 '19 at 07:49
  • @ Paritosh : Oh.. That is pseudo code. It will give you an error because of indent. – owcred601 Dec 02 '19 at 07:50
  • The standard in Python is using [default value](https://stackoverflow.com/questions/13075044/initialize-parameter-of-method-with-default-value), there is no methods overloading, atleast not without third party modules. – Guy Dec 02 '19 at 07:50
  • `D` doesn't exist yet when you attempt to create `C` – Mad Physicist Dec 02 '19 at 07:59
  • @Guy, I read this article, but it is not what I want to get... https://stavshamir.github.io/python/2018/05/26/overloading-constructors-in-python.html – owcred601 Dec 02 '19 at 08:33
  • @owcred601 did u take a look at my answer. your question is not super clear on overloading part. but according to you comment where you say call C() and C(1), my answer could help – Eshaka Dec 02 '19 at 08:46
  • It's completely normal and it is working as intended. Java also calls the parent's constructor. You have to call the `super` constructor as it is pointed out here. https://stackoverflow.com/questions/2967662/any-way-to-not-call-superclass-constructor-in-java – Farhood ET Dec 02 '19 at 08:47
  • another way to do this is use default arguments, most likely `argname=None`. then use if-else with the value of that argument – Eshaka Dec 02 '19 at 08:54
  • @Eshaka, Through your way, I can get the result I want. Thank you. – owcred601 Dec 02 '19 at 09:40

2 Answers2

-2

You have several errors in your code: class not Class, checkout indentation, class definition order... see this:

class D:
    def __init__(self):
        print('it is D')

class C(D):
    def __init__(self):
        print('it is C')

class A:
    def __init__(self):
        self.c = C()

class B:
    def __init__(self):
        self.c = C()

a = A()
b = B()
  • 3
    This doesn't really address the question itself, and just fixes typos and so on. While useful, this is not an answer here and should not have been posted as one. – Paritosh Singh Dec 02 '19 at 07:57
-2

method 1

you can use the *args and **kwargs

args and kwargs

you can get the len(args) and len(kwargs.keys()) (kwargs is a dict) and program different parts depending on the number of arguments. this can work like a constructor overloading

example:

__init__(self, *args):
   if len(args)==0:
      # constructor one 
   if len(args)==1:
      # constructor two

method 2

use default arguments, most likely argname=None. then use if-else with the value of that argument

simple example :

__init__(self, arg1=None):
       if arg1==None:
          # constructor one 
       else:
          # constructor two
Eshaka
  • 974
  • 1
  • 14
  • 38