-3

Given the following classes:

class DataAccessLayer():

    def __init__(self):
        self.engine = create_engine(CONN_STRING, pool_size=95, max_overflow=0)
        self.connection = self.engine.connect()


class DataAccessLayerParallel(DataAccessLayer):

    def __init__(self):
        session_factory = sessionmaker(bind=self.connection)

I try to create an instance of DataAccessLayerParallel as follows:

dal = DataAccessLayerParallel()

However, I get the error:

Exception has occurred: AttributeError
'DataAccessLayerNonParallel' object has no attribute 'connection'

I assume this is because the __init__ method from DataAccessLayer does not run when it is inherited? How would I ensure I can get access to the connection attribute?

Jossy
  • 589
  • 2
  • 12
  • 36

1 Answers1

-1

You need to call the parent constructor with super. Like so:

class DataAccessLayer():

    def __init__(self):
        self.engine = create_engine(CONN_STRING, pool_size=95, max_overflow=0)
        self.connection = self.engine.connect()


class DataAccessLayerParallel(DataAccessLayer):

    def __init__(self):
        super(DataAccessLayerParallel, self).__init__()
        self.session_factory = sessionmaker(bind=self.connection)
Diogo Silva
  • 320
  • 2
  • 14