'% s'% 'somestring'

Here are some examples taken from django-basic-apps :

# self.title is a unicode string already
def __unicode__(self):
        return u'%s' % self.title

# 'q' is a string
search_term = '%s' % request.GET['q']

What is the point of formatting this string?

+5
source share
5 answers

This is just my habit. In these cases, this is not necessary.

+3
source

At first glance, this does not look reasonable, but it has the advantage of forcing the result to be a string (or a unicode string), rather than what it might have been before. Another way to do the same could be to call strin a format argument (or in unicode).

+1
source

, , Nathan Borror, . .

Django - , "" . , i18n/l10n ( , db , , ).

+1

, , python, , .

- / , , , .

0

Another idea: Maybe this was done taking into account possible future implementations? self.title and request.GET [...] currently already have the desired type, but implementation details may change in the future, and they may cease to be a Unicode or string string.

Now I would use str () and unicode (), though ...

0
source

All Articles