% 在 Python 中如何应用于此方法?

发布于 2024-08-05 18:26:06 字数 989 浏览 6 评论 0原文

通过对 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) 是什么意思?它不能是模数,因为 toval 会给我一个字符串。它并不是真正的字符串格式化程序,因为没有另一个百分号。

另外,与此相关:

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

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

发布评论

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

评论(5

梦里的微风 2024-08-12 18:26:06

字符串 % 运算符比您想象的要简单。它的左侧需要一根绳子,右侧需要各种各样的东西。左侧不一定是文字字符串,它可以是变量,或者另一个计算的结果。任何产生字符串的表达式对于 % 的左侧都是有效的。

在第一个示例中, 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.

梦境 2024-08-12 18:26:06

return self.fmt%self.toval(x)

self.fmt

是一个字符串,并且该字符串中可能有一个百分号占位符。格式字符串中的 %r 类似于 %s 但它打印字符串的 repr(),因此它会有引号和反斜杠等等。

% 只是一个运算符,它只是一个方法,与任何其他方法一样,您可以传入文字值或包含值的变量。在您的示例中,他们使用包含格式字符串的变量。

In

return self.fmt%self.toval(x)

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 the repr() 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.

天赋异禀 2024-08-12 18:26:06
def tostr(self, x):
    if x is None: return 'None'
    return self.fmt%self.toval(x)

其中的 % 绝对是一个字符串格式化程序。将格式化程序传递给 tostr 方法,例如 "%s""%r" 看看会发生什么,

我认为 '% csvformat_factory 中的 r' 也是一个字符串格式化程序。 '%r' 表示采用 repr(),这是向用户显示内容的合理方式。我想 format.fmt 在其他地方使用 format.fmt % somevalue

def tostr(self, x):
    if x is None: return 'None'
    return self.fmt%self.toval(x)

The % in this is a string formatter, definitely. Pass the tostr method a formatter, eg "%s" or "%r" to see what happens

I think the '%r' in csvformat_factory is also a string formatter. '%r' means take the repr() which is a reasonable way to display something to a user. I imagine that format.fmt is used elsewhere format.fmt % somevalue.

放肆 2024-08-12 18:26:06

代码:
return self.fmt % self.toval(x)

是 % 运算符的“字符串格式化”使用,就像您怀疑的那样。

该类传递 format,它是一个包含格式的字符串,当调用 tostr(x) 时,它将返回字符串 % x。

这就像直接使用 % 一样,只是保存格式字符串供以后使用。换句话说,不是做:

"I want to print the number: %n" % 20

正在发生的是:

format_str = "I want to print the number: %n"
x = 20
print format_str % 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:

"I want to print the number: %n" % 20

What's happening is:

format_str = "I want to print the number: %n"
x = 20
print format_str % x

Which is exactly the same thing.

不爱素颜 2024-08-12 18:26:06

% 在字符串格式化中有不止一种用途。一种用途是在 %s、%d 等中。

另一种用途是将“使用 %d 和 %s 的字符串”与 int-value 和 string-value 分开。

例如,

'string in which we use %d and %s' % (17, 'blue')

我们可以将

'string in which we use 17 and blue'

“我们使用 %d 和 %s 的字符串”存储在变量中,

a = 'string in which we use %d and %s'

然后

a % (17, 'blue')

导致在

'string in which we use 17 and blue'

您的示例中
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

'string in which we use %d and %s' % (17, 'blue')

would result in

'string in which we use 17 and blue'

we could store 'string in which we use %d and %s' in a variable,

a = 'string in which we use %d and %s'

then

a % (17, 'blue')

results in

'string in which we use 17 and blue'

In your example
self.fmt%self.toval(x)

self.fmt is similar to a above and self.toval(x) is (17, 'blue')

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