Florian Fasmeyer
Florian Fasmeyer

Reputation: 879

Shall I override default fields using inheritance?

EDIT: ChatterOne's answer is good. I must keep it plain simple. No bizarre/wacky behavior is worth to avoid repeating a simple, explicit and useful variable.

What I wanted (pseudo-code example): Getting rid of "server" parameters.

Config.default_values.server = Server.application
class ApplicationDbConfig(Enum):
    database_1 = Config(Database.db1)
    database_2 = Config(Database.db2)
    ...


Config.default_values.server = Server.warehouse
class WarehouseDatabaseConfig(Enum):
    database_1 = Config(Database.db1)
    database_2 = Config(Database.db2)
    ...

What best solution is:

Keep your variables explicit. no need to hide it or change a state internally. Explicit and clear is better. So I shall abandon my objective of getting rid of s and keep it simple.

s = Server.application
class ApplicationDatabaseConfig(Enum):
    database_1 = Config(Database.db1, s)
    database_2 = Config(Database.db2, s)
    ...


s = Server.warehouse
class WarehouseDatabaseConfig(Enum):
    database_1 = Config(Database.db1, s)
    database_2 = Config(Database.db2, s)
    ...

The original question was: Is it worth it to use inheritance to override default parameters? Or do you have a better way to do it?

This is the original code I used to do this, using inheritance. It took too much space and added a lot of noise for the viewer.

@dataclass
class Config:
    database: Database
    server: Server
    driver: Driver = Driver.sql_serv_native_client_11
    is_trusted: bool = True
    additional_params: str = None


@dataclass
class ApplicationConfig(Config): server: Server = Server.application
    
@dataclass
class WarehouseConfig(Config): server: Server = Server.warehouse

...

class ApplicationDatabasesConfig(Enum):
    database_1 = ApplicationConfig(Database.db1)
    database_2 = ApplicationConfig(Database.db2)
    ...


class WarehouseDatabasesConfig(Enum):
    database_3 = WarehouseConfig(Database.db3)
    database_4 = WarehouseConfig(Database.db4)
    ...

Upvotes: 1

Views: 61

Answers (1)

ChatterOne
ChatterOne

Reputation: 3541

Assuming that what you want is to instantiate the same class with different parameters, except for a few that may or may not have a default value, I'd do something like this (not tested, just quickly typed), also assuming you have a default driver:

class Config:
    def __init__(self, database, server=Server.serverX, driver=Driver.default, is_trusted=True, additional_params=None):
        self.database = database
        self.driver = driver
        self.server = server
        self.is_trusted = is_trusted
        self.additional_params = additional_params

Then you can use it like:

database_1 = Config(Database.db1)
database_2 = Config(Database.db2)
database_3 = Config(Database.db3)
database_4 = Config(Database.db4, Server.serverY)

But this looks pretty straight-forward to me, so maybe I'm missing something.

BTW, you can also use it with named params, something like

database_4 = Config(database=Database.db4, server=Server.serverY)

if you want to be a bit more verbose/explicit (again, not tested, there may be typos in there).

Upvotes: 2

Related Questions