带有变量的Python字符串

发布于 2025-02-08 20:30:47 字数 333 浏览 1 评论 0原文

我正在尝试做这样的事情:

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 技术交流群。

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

发布评论

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

评论(2

生活了然无味 2025-02-15 20:30:47

我会说定义A __调用__方法以及一些字符串格式化

class SomeEnum(str, Enum):
    STRING_A = 'This is a string A with variable %s %s '
    STRING_B = 'This is a string B with variable %s %s'

    def __call__(self, *args):
        return self % args

print(SomeEnum.STRING_A("a", "b"))  # This is a string A with variable a b
print(SomeEnum.STRING_B("c", "d"))  # This is a string B with variable c d

唯一规则:提供相同数量的paramater,因为字符串期望


动态格式

class SomeEnum(str, Enum):
    STRING_A = 'This is a string A with variable '
    STRING_B = 'This is a string B with variable '

    def __call__(self, *args):
        if len(args) == 1:
            return self + args[0]
        return self + ', '.join(args[:-1]) + " and " + args[-1]


print(SomeEnum.STRING_A("a"))            # This is a string A with variable a
print(SomeEnum.STRING_B("a", "b"))       # This is a string B with variable a and b
print(SomeEnum.STRING_B("a", "b", "c"))  # This is a string B with variable a, b and c

I'd say define a __call__ method along with some string formatting

class SomeEnum(str, Enum):
    STRING_A = 'This is a string A with variable %s %s '
    STRING_B = 'This is a string B with variable %s %s'

    def __call__(self, *args):
        return self % args

print(SomeEnum.STRING_A("a", "b"))  # This is a string A with variable a b
print(SomeEnum.STRING_B("c", "d"))  # This is a string B with variable c d

The only rule : provide the same amount of paramater, as the string expects


Dynamic formatting

class SomeEnum(str, Enum):
    STRING_A = 'This is a string A with variable '
    STRING_B = 'This is a string B with variable '

    def __call__(self, *args):
        if len(args) == 1:
            return self + args[0]
        return self + ', '.join(args[:-1]) + " and " + args[-1]


print(SomeEnum.STRING_A("a"))            # This is a string A with variable a
print(SomeEnum.STRING_B("a", "b"))       # This is a string B with variable a and b
print(SomeEnum.STRING_B("a", "b", "c"))  # This is a string B with variable a, b and c
丢了幸福的猪 2025-02-15 20:30:47

不!。你不能那样做。这是不切实际的。您正在编写的代码看不到Intellisense。如果您想在公共场合看到它,则可以使用self

class SomeEnum:

    def __init__(self, enum):
        self.Enum = enum
        self.STRING_A = f'This is a string A with variable {enum}'
        self.STRING_B = f'This is a string B with variable {enum}'


num = 6

p1 = SomeEnum(num)

print(p1.STRING_A)
print(p1.STRING_B)

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 use self.

class SomeEnum:

    def __init__(self, enum):
        self.Enum = enum
        self.STRING_A = f'This is a string A with variable {enum}'
        self.STRING_B = f'This is a string B with variable {enum}'


num = 6

p1 = SomeEnum(num)

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