2

I have a python project with the following structure:

class A:
    def __init__(self):
       # Long startup

class B(A):
    def __init__(self):
        A.__init__(self)

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

The problem is that A takes a long time to initialize, and I don't want to have to run it twice. Is it possible to "skip" the __init__ of A on any occurrence after the first one?

I know that I can inherit B in C (rather than A), but that doesn't seem natural, seeing as B and C are completely different parts of my program.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    How do instances of `B` and `C` interact? Why can't you keep track of how many times one of them has been initialized? Is it not possible to do `A.__init__` only within `B` and not `C`? It's a little hard to give you an answer without knowing – IanQ Feb 20 '19 at 02:59
  • 2
    Depends on that A is doing. It might make sense for it to either have lazy initialization or share something that only one created for A between all its instances as a class variable. – Dan D. Feb 20 '19 at 03:04
  • Is [this](https://stackoverflow.com/questions/15226721/python-class-member-lazy-initialization) what you had in mind? – python_crypto_questions Feb 20 '19 at 03:13
  • 1
    Yes, but it might not be appropriate or applicable to your situation. – Dan D. Feb 20 '19 at 03:27
  • I see. it seems to work. Thank you. – python_crypto_questions Feb 20 '19 at 03:28

0 Answers0