Wednesday, January 25, 2012

Re: django docs __unicode__ return u'%s %s

Just to fill in some more info, __unicode__ methods are also part of
core python and just happen to appear a lot in django code because of
its preference for 16-bit unicode strings (though I think the methods
are actually mostly invoked in the admin). There is also a __str__
function for "ordinary" (8 bit potentially mbcs) strings. (This is
Python 2.x; Python 3.x is somewhat different.)

User-defined __unicode__ methods are overrides of builtin methods that
all objects are guaranteed to have. As the __...__ in the name
suggests, these methods are called implicitly in certain kinds of
expressions. Two cases I know of in Python 2.7 are expressions of
form unicode(obj) and expressions of form u'...%s...' % obj (and I
assume other format expression tyopes where the format string is
unicode). In both, obj is converted to a unicode string using
whatever __unicode__ method is defined for the object.


On Jan 24, 6:04 pm, Krondaj <c.d.smi...@gmail.com> wrote:
> Hi,
>
> in the django docs about __unicode__  it says the following:
>
> class Person(models.Model):
>     first_name = models.CharField(max_length=50)
>     last_name = models.CharField(max_length=50)
>
>     def __unicode__(self):
>         return u'%s %s' % (self.first_name, self.last_name)
>
> what does the u'%s %s' % mean... I cannot find any exaplanation of
> this in the docs?
>
> i've seen this in someones code that was kindly lent to me by one of
> the RC chat room people:
>
> return u'ID%s: %s - %s - %s - %s' % (self.id, self.user,
> self.question, self.answer, self.get_status_display())
>
> but all this u' %s  %s'   %%%sss or what ever is most confusing....
>
> is there a document or help guide some where that explains this
> nomenclature, or method, system???
>
> as I believe if you take the top example you should be able to do:
>  def __unicode__(self):
>         return(self.first_name, self.last_name)
>
> If there is no documentation (for dummies) can anyone explain it to
> me??
>
> Thanks
>
> Krondaj

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate