views:

167

answers:

5

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 =)

+5  A: 

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.

Ned Batchelder
A: 
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.

Nick Craig-Wood
A: 

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.

Edan Maor
thanskso lets say we have format.fmt = %rso using the class(FormatFormatStr) and method tostr, we would eventually get '%r%format.toval(x), and supposedly x could be int or str or some format like that right? That seems to make sense.
Pete
and that fmt would be whatever the toval(x) brings up.
Pete
+3  A: 

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.

yjerem
A: 

% 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')

foosion