% 在 Python 中如何应用于此方法?
通过对 python 的研究,我发现 % 的两种用途。它可以用作所谓的模数,这意味着它将把左边的值和右边的值相除,然后吐出余数。
另一个用途是字符串格式化程序。所以我可以做类似 'Hi there %s' % name
的操作,其中 name 是名称列表。
此外,如果您在字符串格式中看到 %%
,则意味着将输入文字 %
。
这是我的问题,我发现了这个:
class FormatFormatStr(FormatObj):
def __init__(self, fmt):
self.fmt = fmt
def tostr(self, x):
if x is None: return 'None'
return self.fmt%self.toval(x)
return self.fmt%self.toval(x)
是什么意思?它不能是模数,因为 tova
l 会给我一个字符串。它并不是真正的字符串格式化程序,因为没有另一个百分号。
另外,与此相关:
def csvformat_factory(format):
format = copy.deepcopy(format)
if isinstance(format, FormatFloat):
format.scale = 1. # override scaling for storage
format.fmt = '%r'
return format
format.fmt = '%r'
中的百分比意味着什么,这意味着插入一个 repr()
字符串吗?或者这是否意味着插入变量 r 代表的内容?整个程序中的r
也指的是重组。
谢谢大家。希望这是有道理的 =)
From my studying of python, I've found two uses for %. It can be used as what's called a modulo, meaning it will divide the value to the left of it and the value to the right of it and spit back the remainder.
The other use is a string formatter. So I can do something like 'Hi there %s' % name
, where name is a list of names.
Also, if you see %%
in a string formatting, that means a literal %
will be entered.
Here is my question, I found this:
class FormatFormatStr(FormatObj):
def __init__(self, fmt):
self.fmt = fmt
def tostr(self, x):
if x is None: return 'None'
return self.fmt%self.toval(x)
What does return self.fmt%self.toval(x)
mean? It can't be a modulo because tova
l will give me a string. It's not really a string formatter because there isn't another percent sign.
also, related to this:
def csvformat_factory(format):
format = copy.deepcopy(format)
if isinstance(format, FormatFloat):
format.scale = 1. # override scaling for storage
format.fmt = '%r'
return format
What does the percent mean in format.fmt = '%r'
does this mean to insert a string a la repr()
? Or does it mean insert what the variable r
represents? r
in this overall program also refers to a recarray.
Thanks everyone. Hope this makes sense =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
字符串 % 运算符比您想象的要简单。它的左侧需要一根绳子,右侧需要各种各样的东西。左侧不一定是文字字符串,它可以是变量,或者另一个计算的结果。任何产生字符串的表达式对于 % 的左侧都是有效的。
在第一个示例中, self.fmt 是一个字符串。为了在这种情况下有用,它应该有一个百分号。
在第二个示例中,
format.fmt
被设置为一个字符串,该字符串可用作 % 的左侧。在这种情况下,“%r”意味着,将值的 repr() 插入到字符串中,正如您所说的那样。The string % operator is simpler than you are imagining. It takes a string on the left side, and a variety of things on the right side. The left side doesn't have to be a literal string, it can be a variable, or the result of another computation. Any expression that results in a string is valid for the left side of the %.
In your first example,
self.fmt
is a string. In order to be useful in this context, it should have a percent sign in it.In your second example,
format.fmt
is being set to a string that would be useful as the left side of the %. In this case, "%r" means, insert the repr() of the value into the string, as you have said.中
self.fmt
是一个字符串,并且该字符串中可能有一个百分号占位符。格式字符串中的
%r
类似于%s
但它打印字符串的repr()
,因此它会有引号和反斜杠等等。%
只是一个运算符,它只是一个方法,与任何其他方法一样,您可以传入文字值或包含值的变量。在您的示例中,他们使用包含格式字符串的变量。In
self.fmt
is a string, and that string presumably has a percent-sign placeholder in it.%r
in a format string is like%s
but it prints therepr()
of the string, so it'll have quotes and backslashes and all that.%
is just an operator which is just a method, and like any other method you can either pass in a literal value or a variable containing a value. In your examples they use a variable containing the format string.其中的
%
绝对是一个字符串格式化程序。将格式化程序传递给tostr
方法,例如"%s"
或"%r"
看看会发生什么,我认为
'%
也是一个字符串格式化程序。csvformat_factory
中的 r''%r'
表示采用repr()
,这是向用户显示内容的合理方式。我想format.fmt
在其他地方使用format.fmt % somevalue
。The
%
in this is a string formatter, definitely. Pass thetostr
method a formatter, eg"%s"
or"%r"
to see what happensI think the
'%r'
incsvformat_factory
is also a string formatter.'%r'
means take therepr()
which is a reasonable way to display something to a user. I imagine thatformat.fmt
is used elsewhereformat.fmt % somevalue
.代码:
return self.fmt % self.toval(x)
是 % 运算符的“字符串格式化”使用,就像您怀疑的那样。
该类传递 format,它是一个包含格式的字符串,当调用 tostr(x) 时,它将返回字符串 % x。
这就像直接使用 % 一样,只是保存格式字符串供以后使用。换句话说,不是做:
正在发生的是:
这是完全相同的事情。
The code:
return self.fmt % self.toval(x)
Is the "string formatting" use of the % operator, just like you suspected.
The class is handed format, which is a string containing the formatting, and when tostr(x) is called, it will return the string % x.
This is just like using % directly, only with saving the format string for later. In other words, instead of doing:
What's happening is:
Which is exactly the same thing.
% 在字符串格式化中有不止一种用途。一种用途是在 %s、%d 等中。
另一种用途是将“使用 %d 和 %s 的字符串”与 int-value 和 string-value 分开。
例如,
我们可以将
“我们使用 %d 和 %s 的字符串”存储在变量中,
然后
导致在
您的示例中
self.fmt%self.toval(x)
self.fmt 与上面类似, self.toval(x) 是 (17, 'blue')
% has more than one use in string formatting. One use is in %s, %d, etc.
Another use is to separate 'string in which we use %d and %s' from int-value and string-value.
For example
would result in
we could store 'string in which we use %d and %s' in a variable,
then
results in
In your example
self.fmt%self.toval(x)
self.fmt is similar to a above and self.toval(x) is (17, 'blue')