Python super() 和继承的 class 目录位置
Python super() and inherited class directory location
我正在尝试弄清楚如何让我正在使用的系统为我正在处理的 UI 项目的主题管理器正常工作。
示例:
文件结构:
element.py - class Element()
button.py - class Button(Element)
text.py - class Text(Element)
input/text.py - class Text(Element)
我在主题中使用 class-name 来表示不同的属性。 (类似于 CSS)
所以'element'可以让文本颜色为白色。
我目前有所有 UI 元素继承自 'Element' 并使用:
self.__class__.__name__.lower()
这适用于 'Button' 和 'Element',问题在 input.text 与文本中发挥作用。 Text 用于显示文本的所有用途,因此它几乎存在于所有元素中,但是 input.text class 被命名为 Text 也用于文本输入,我想使用 属性 选择器,例如'input.text' 而不是我现在用 class.name.
得到的 'text'
我正在尝试最终设置它,以便它有一个插件系统供其他人添加元素并能够使用目录结构作为 属性 选择器的一部分。
例如:
Button class: button
Text class: text
Text Input class: text, but in the directory 'input'
我想弄清楚如何让 python 检查来自 super() 调用的元素中的 'self' 是否在 'element.py' 所在的目录中。这会将目录名称添加到它检查的属性的 class 名称中。
最终结果将是:
element
button
text
input.text
TLDR:我需要知道如何检查继承的 class 是否位于与基础 class 不同的目录中,以及该目录的名称。
感谢您帮助解决这个问题。
好吧,我找到了如何按照我想要的方式去做。只是回答问题,同时我也在思考@supersam654 的评论。
我能够找出如何从继承的 class 中获取路径:
import os, sys
el = os.path.dirname(sys.modules[self.__class__.__module__].__file__).split('/')
base = os.path.dirname(__file__).split('/')
for i in base:
el.remove(i)
'el' 中剩余的任何内容都将具有我可以使用的单独目录名称。
我正在尝试弄清楚如何让我正在使用的系统为我正在处理的 UI 项目的主题管理器正常工作。
示例:
文件结构:
element.py - class Element()
button.py - class Button(Element)
text.py - class Text(Element)
input/text.py - class Text(Element)
我在主题中使用 class-name 来表示不同的属性。 (类似于 CSS)
所以'element'可以让文本颜色为白色。
我目前有所有 UI 元素继承自 'Element' 并使用:
self.__class__.__name__.lower()
这适用于 'Button' 和 'Element',问题在 input.text 与文本中发挥作用。 Text 用于显示文本的所有用途,因此它几乎存在于所有元素中,但是 input.text class 被命名为 Text 也用于文本输入,我想使用 属性 选择器,例如'input.text' 而不是我现在用 class.name.
得到的 'text'我正在尝试最终设置它,以便它有一个插件系统供其他人添加元素并能够使用目录结构作为 属性 选择器的一部分。
例如:
Button class: button
Text class: text
Text Input class: text, but in the directory 'input'
我想弄清楚如何让 python 检查来自 super() 调用的元素中的 'self' 是否在 'element.py' 所在的目录中。这会将目录名称添加到它检查的属性的 class 名称中。
最终结果将是:
element
button
text
input.text
TLDR:我需要知道如何检查继承的 class 是否位于与基础 class 不同的目录中,以及该目录的名称。
感谢您帮助解决这个问题。
好吧,我找到了如何按照我想要的方式去做。只是回答问题,同时我也在思考@supersam654 的评论。
我能够找出如何从继承的 class 中获取路径:
import os, sys
el = os.path.dirname(sys.modules[self.__class__.__module__].__file__).split('/')
base = os.path.dirname(__file__).split('/')
for i in base:
el.remove(i)
'el' 中剩余的任何内容都将具有我可以使用的单独目录名称。