带有变量的Python字符串
我正在尝试做这样的事情:
class SomeEnum(str, Enum):
STRING_A = 'This is a string A with variable ' + variable
STRING_B = 'This is a string B with variable ' + variable
例如,将其像模板一样使用:
some_list.append(SomeEnum.STRING_A(variable))
这是否可以在枚举中进行,因为它违反了枚举的目的?还是您还建议枚举什么?多谢!
I am trying to do something like this:
class SomeEnum(str, Enum):
STRING_A = 'This is a string A with variable ' + variable
STRING_B = 'This is a string B with variable ' + variable
and use it like a template, for instance:
some_list.append(SomeEnum.STRING_A(variable))
is this even possible in Enums since it defies the purpose of Enums a bit? Or what else would you suggest instead of Enums? Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会说定义A
__调用__
方法以及一些字符串格式化唯一规则:提供相同数量的paramater,因为字符串期望
动态格式
I'd say define a
__call__
method along with some string formattingThe only rule : provide the same amount of paramater, as the string expects
Dynamic formatting
不!。你不能那样做。这是不切实际的。您正在编写的代码看不到
Intellisense
。如果您想在公共场合看到它,则可以使用self
。No!. You can't do like that. It is not practical. The code you're writing cannot see
intellisense
. If you want to see it in public, then you can useself
.