我正在创建一个库,其中一部分需要一个结构化的模型。
假设我们有各个部分,每个部分都可以有小节(类别)。所有这些都符合此模型:
class Section:
sec_property = ""
class Category:
sub_property = ""
class General:
sec_property = ""
sub_property = ""
现在,我想拥有多个部分,每个部分都应具有自定义类别,甚至应该参考特定类别。每个类别都从其部分继承,因此它可以访问父母的 sec_property
。
这就是我现在所拥有的,但是我面临着一些问题。
class CARS(General):
sec_property = "car"
CAR1 = None
class CAR1(CARS):
sub_property = "car1"
CARS.CAR1 = CAR1
我希望只能通过 CARS
- &gt访问 car1
; cars.car1
。目前的问题是,它充当班级的属性,而不是班级本身,这意味着IDE无法正确识别它。
为什么我想要继承和常规类:我希望能够做 anyclass.sub_property
,并且应该返回空字符串或属性本身。我也希望能够执行 anyCategory.sec_property
,并且应该具有父类的属性(因此继承)。
我想避免复制数据和其他不良实践。
在此示例
# sections.py
class MOTOR(PARTS):
sub_property = "motor"
class PARTS(General):
sec_property = "parts"
MOTOR = MOTOR
class CAR1(CARS):
sub_property = "car1"
class CARS(General):
sec_property = "car"
CAR1 = CAR1
PARTS = PARTS
中,CAR1是班级本身的别名,而不是属性,常数或任何事物。问题是,在这种情况下,它不能从 cars
继承,我非常希望这样做。
我希望能够从各节本身访问所有类别(子段),并能够访问父属性(除非被覆盖,否则该部分将有另一部分作为参考)。
我也不想创建重复(而不是继承属性 sec_property
将其分配给每个类别),因为这并不是一个很好的做法。
# some other .py
import sections
sections.CARS.CAR1.sec_property
sections.CARS.CAR1.sub_property
sections.CARS.PARTS.MOTOR.sec_property
sections.CARS.PARTS.MOTOR.sub_property
sections.PARTS.MOTOR.sec_property
# etc etc
我也不想直接访问类别( car1
电动机),但是我已经弄清楚了一个 - 创建 __ __ init__。 py
,包括我想要的东西,不包括其余的东西。
澄清
我只需要作为特殊的数据级,枚举或类似的东西访问这些内容。我不需要做它们的实例。
我正在寻找一个可以正确识别的解决方案。我当前的解决方案(带有 = none
的第二个CodeBlock)在运行时没有错误的工作,但是IDE不知道我正在使用什么,因此无建议。 https://i.sstatic.net/xvzeh.png //i.sstatic.net/47jtx.png“ rel =“ nofollow noreferrer”> https://i.sstatic.net/47jtx.png
信息有关我如何生成文件的信息:首先我制作所有定义没有连接(仅继承),最后我连接所有内容,因此它可以正确链接。
有问题的特定文件
I'm creating a library, and one part of it requires a structured model.
Let's say we have sections, and each section can have subsections (categories). All of them fit this model:
class Section:
sec_property = ""
class Category:
sub_property = ""
class General:
sec_property = ""
sub_property = ""
Now, I want to have multiple sections, and each one should have custom categories, or even other sections, which would reference the specific one. Each category inherits from its section, so it has access to the sec_property
of the parent.
This is what I have right now, but I'm facing some issues with it.
class CARS(General):
sec_property = "car"
CAR1 = None
class CAR1(CARS):
sub_property = "car1"
CARS.CAR1 = CAR1
I want to be able to access CAR1
only through CARS
-> CARS.CAR1
. The issue right now is, that it acts as a property of the class, not a class itself, meaning the IDE doesn't recognize it properly.
Why I want the inheritance, and General class: I want to be able do anyClass.sub_property
, and it should either return the empty string or the property itself. I also want to be able to do anyCategory.sec_property
, and it should have the property of the parent class (hence the inheritance).
I want to avoid duplicating data and other bad practices.
The goal
# sections.py
class MOTOR(PARTS):
sub_property = "motor"
class PARTS(General):
sec_property = "parts"
MOTOR = MOTOR
class CAR1(CARS):
sub_property = "car1"
class CARS(General):
sec_property = "car"
CAR1 = CAR1
PARTS = PARTS
In this example, the CAR1 is an alias to the class itself, not a property, constant or anything. The issue is, that it can't inherit from CARS
in this case, which I would very much like to do.
I want to be able to access all categories (sub-sections) from the sections themselves, and be able to access the parent attributes (unless they are overridden, where a section would have another section as reference).
I also don't want to create duplicates (instead of inheriting the attribute sec_property
assign it to each category), as that's not really a great practise.
# some other .py
import sections
sections.CARS.CAR1.sec_property
sections.CARS.CAR1.sub_property
sections.CARS.PARTS.MOTOR.sec_property
sections.CARS.PARTS.MOTOR.sub_property
sections.PARTS.MOTOR.sec_property
# etc etc
I also don't want to be able to access the categories (CAR1
, MOTOR
) directly, but I've already figured that one out - create __init__.py
and include the stuff I want, and don't include the rest.
Clarifications
I only need to access these as special dataclasses, or enums, or something like that. I don't need to make instances of them ever.
I'm looking for a solution that IDEs recognize properly. My current solution (second codeblock with the = None
) works without errors at runtime, but the IDE has no idea what I'm working with, therefore can't suggest anything. https://i.sstatic.net/XVZeh.png https://i.sstatic.net/47JTX.png
Info on how I generate the file: First I make all the definitions with no connections (only inheritance), and at the end, I connect everything, so it links properly.
Specific file in question
https://github.com/Mahrkeenerh/apbi/blob/4d7bce2f297594e8d9a2798833c628bc9cd7c1f3/apbi/models/sections.py
发布评论
评论(1)
请使用类型提示。
使用 forthrof refortions (string string criless):在程序上验证注释, :
严格来说,类型提示应为
可选[类型[“类型”]
:Use type hints with forward references (string literals):
Verifying the annotations programmatically:
Strictly speaking, the type hints should be
Optional[Type["TYPE"]
: