Python 类依赖模板?

发布于 2024-09-25 11:29:06 字数 545 浏览 0 评论 0原文

我想根据对象的类创建一个小部件,在 mako 中有一种简单的方法可以做到这一点吗?例如,

类 A 可能具有属性 A 和 B,

类 B 可能具有属性 A、B 和 C,

是否有这样的模式?

我想创建一个他们继承的超类,但是如果我有一个函数 print 并通过调用 obj.print() 来调用它,但我不想将模板代码放入类函数中,

例如,我有一个小部件可以

<div>obj.a</div>
<div>obj.b</div>
<div>obj.c</div>
<div>obj.d</div>

,但如果它是对象 B,我希望它去

<div>obj.a</div>
<div>obj.b</div>
<div>obj.c</div>

等等,但想知道是否有一个干净的方法来做到这一点

i want to create a widget depending on the class of the object, is there a simple way to do that in mako? for example

class A might have attributes A and B

while

class B might have attributes A, B and C

is there a pattern for this?

i want to make a super class that they but inherit, but if I have a function print and call it by calling obj.print(), but i don't want to put template code into class functions

for example, i have a widget that goes

<div>obj.a</div>
<div>obj.b</div>
<div>obj.c</div>
<div>obj.d</div>

but if it's object B, i want it to go

<div>obj.a</div>
<div>obj.b</div>
<div>obj.c</div>

etc, but was wondering if theres a clean way to do it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以歌曲疗慰 2024-10-02 11:29:06

创建一个在 A 类中返回 [a, b] 并在 B 类中返回 [a, b, c] 的方法。

然后你可以这样做:(

% for stuff in thing.return_list_of_stuff:
    <div>${stuff}</div>
% endfor

我从未使用过mako,因此语法可能不正确。)

Make a method which returns [a, b] in class A and [a, b, c] in class B.

Then you can do:

% for stuff in thing.return_list_of_stuff:
    <div>${stuff}</div>
% endfor

(I've never used mako, so the syntax might be incorect.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文