简体   繁体   English

在python中访问后在列表内重新出现字符串格式

[英]Reoccurring string-formatting within a list upon access in python

In python, is it possible to have a reoccurring string-formatting operation take place when an item in a list is accessed? 在python中,访问列表中的项目时是否可能会发生重复的字符串格式化操作?

For example: 例如:

>>>from random import randint
>>>a = ["The random number is: {0}".format(randint(0,10))]
>>>print a[0]
The random number is: 3
>>>print a[0]
The random number is: 3

Obviously it's obtaining a random integer, formatting the string and saving it in the list when the list is first defined. 显然,它是在获取随机整数,格式化字符串并将其首次定义列表时保存在列表中。 Performance hit aside, I'd like to know if it is possible to override this behavior. 除了性能之外,我想知道是否有可能重写此行为。

I know if I were to see this question I would respond with something like "you're doing it wrong" and would provide something similar to the below answer... 我知道如果我看到这个问题,我会回答“您做错了”,并提供类似于以下答案的内容...

>>>a = ["The random number is: {0}"]
>>>print a[0].format(randint(0,10))

But lets assume that's not a solution for this question. 但是,让我们假设这不是该问题的解决方案。 I'd really like for the formatting to be defined and take place in the list itself (if possible). 我真的很想定义格式并在列表本身中进行(如果可能的话)。

Another example: 另一个例子:

a = ["The some sweet string: {0}".format(someFunction),
     "Another {0} different string {1}".format(someFunctionTwo, someFunctionThree)]

Where someFunction* provides a "random" result upon each call. 其中someFunction *在每次调用时提供“随机”结果。

I know its a bit of a stretch and I may have to rely on the methods provided already ( thanks for your feedback ) but, I figured I'd give it a shot. 我知道这有点困难,我可能不得不依靠已经提供的方法(感谢您的反馈),但是,我想我可以尝试一下。

Thanks again. 再次感谢。

It's better to use a function for this: 最好为此使用一个函数:

In [1]: from random import randint

In [2]: def func():
   ...:     return "The random number is: {0}".format(randint(0,10))
   ...: 

In [3]: func()
Out[3]: 'The random number is: 7'

In [4]: func()
Out[4]: 'The random number is: 2'

In [5]: func()
Out[5]: 'The random number is: 3'

You can create a class and override __str__ : 您可以创建一个类并覆盖__str__

>>> from random import randint
>>> class Foo(object):
...     def __str__(self):
...        return "The random number is: {0}".format(randint(0,10))
... 
>>> a = [Foo()]
>>> print a[0]
The random number is: 8
>>> print a[0]
The random number is: 10
>>> print a[0]
The random number is: 5 

But you're right, my first inclination is to say that you're probably doing it wrong... 但是你是对的,我的第一个倾向是说你可能做错了...


Here's another idea -- keep your lists with format strings in them: 这是另一个想法-将列表中的格式字符串保留在其中:

a = ["The some sweet string: {func1}",
     "Another {func2} different string {func3}"]

for item in a:
   print item.format(func1=func1(),func2=func2(),func3=func3())

Obviously this isn't efficient (as you call functions when you don't necessarily need them ...), but it could work. 显然,这并不高效(因为您在不一定需要它们时调用函数...),但是它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM