How do I go about overriding a parent class attribute in a child class without using object instances of either class? Coming from the world of Java/C++ and their strict structural designs, I'm finding myself challenged by Python's way of doing things. I'd like to stay relatively static.
Example:
from urllib.parse import urljoin
class base:
host = "/host/"
path = "Override this in child classes"
url = urljoin(host, path)
class config(base):
path = "config"
@classmethod
def print_url(cls):
print(cls.url) # Currently prints "/host/Override this in child classes"
# Would like to print "/host/config" instead
class log(base):
path = "log"
@classmethod
def print_url(cls):
print(cls.url) # Currently prints "/host/Override this in child classes"
# Would like to print "/host/log" instead
Desired Usage:
>>> config.print_url()
/host/config
>>> log.print_url()
/host/log
I'd like the config.path and log.path attributes to override base.path. This is so I can use url = urljoin(host, path) once and for all in the base class (and avoid having to copy/paste that same attribute/calculation in every single derived class).
I can't figure out how to accomplish this without constructing objects (which I'm hoping to avoid). Anyone have any advice? Thanks in advance!