Hi Petr,

I'm using python 2.7.2

for another model i'm forming i have:

def __unicode__(self):
    return (self.project_and_task, self.project_number,
self.date_created, self.requested_by)

to understand (i think the new method makes a little more sense?) the
format should be:

def __unicode__(self):
    return u'{}  {}  {}  {}'.format(self.project_and_task,
self.project_number, self.date_created, self.requested_by)

is the two spaces between each {} two seperate the individual strings
with two spaces?

Krondaj





On Jan 25, 11:11 am, "Petr Přikryl" <prik...@atlas.cz> wrote:
> > "coded kid" wrote:
> >If I may say, 'u' makes it easy to return variable with any hassle.
> >And %s rep each string in your script. I think there should be other
> >ways to go about this. Hope you get my point?
>
> This is wrong.  The prefix 'u' means that the following string is a Unicode
> string, nothing more or less.  It is a fact for Python 2.x.  
> Seehttp://docs.python.org/reference/lexical_analysis.html#string-literals
>
> The % character after the unicode string means "string interpolation",
> i.e. replacing the placeholders inside the string template on the left.
> See the String Formatting 
> Operationshttp://docs.python.org/library/stdtypes.html#string-formatting-operat...
> You will also find the placeholder types there.
>
> >> > Krondaj wrote:
> >> > > what does the u'%s %s' % mean... I cannot find any exaplanation of
> >> > > this in the docs?
>
> The .format() method of the string type is the newer one, introduced
> in Python 2.6.  It is the preferred way now.  But you can use it only
> when you can be sure that the Python is 2.6 or newer. The command
> in your __unicode__ method would look like:
>
> def __unicode__(self):
>         return u'{0}  {1}'.format(self.first_name, self.last_name)
>
> The '{0}' is the placeholder for the self.first_name (zero'th argument,
> the '{1}' is for the first argument (self.last_name), etc.  Since Python 2.7
> (if I recall correctly) you need not to use the numbers inside:
>
> def __unicode__(self):
>         return u'{}  {}'.format(self.first_name, self.last_name)
>
> You do not want to return only...
>
> def __unicode__(self):
>         return (self.first_name, self.last_name)
>
> ... because you want the method to return the unicode string representation.
> Without .format() or without the % the method would return a tuple with
> the information, not the string.
>
> Petr

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to