我试图访问父类的实例变量作为子类中的类变量.
其目的是父类将有许多子类,这些子类都需要具有相同的 struct ,许多不同的人将使用和创建这些子类,而不需要知道父类的内部工作方式.
class Human(ABC):
def __new__(cls, *args, **kwargs):
cls.human_name = args[0]
cls.source = f'database_{cls.__name__}'.lower()
return super().__new__(cls)
@property
@abstractmethod
def query(self):
pass
class Company:
class Employee(Human):
query = f'SELECT {human_name} FROM {source};'
# these two functions are just for testing and will not be in the final product
def print_something(self):
print(self.human_name)
def print_source(self):
print(self.source)
e = Company.Employee('John')
print(e.human_name)
print(e.query)
e.print_source()
我希望能够创建父类Human的子类(在公司中一起构建),其中我只需要定义查询变量,该变量应自动识别变量human_name
和source
.
我该如何让这件事尽可能简单?这可能吗?