The '%' symbol is used for formatting inside of a string. This is Python, not Django specific. It is a shortcut to insert variables (and format them to your liking) instead of doing messy stuff like this:
adjective = 'messy' print 'This is the ' + adjective + ' way to do it'
As opposed to: adjective = 'preferred' print 'This is the %s way to do it' % adjective
On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.smi...@gmail.com> wrote: > what does the u'%s %s' % mean... I cannot find any exaplanation of > this in the docs?
It's not covered in Django's documentation because it's a standard feature of the Python programming language -- this is basic Python string formatting, which follows similar conventions to printf()-style formatting in other languages.
-- "Bureaucrat Conrad, you are technically correct -- the best kind of correct."
On Tue, 2012-01-24 at 18:33 -0600, James Bennett wrote: > On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.smi...@gmail.com> wrote: > > what does the u'%s %s' % mean... I cannot find any exaplanation of > > this in the docs?
> It's not covered in Django's documentation because it's a standard > feature of the Python programming language -- this is basic Python > string formatting, which follows similar conventions to printf()-style > formatting in other languages.
soon to be deprecated in favour of using format. -- regards Kenneth Gonsalves
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?
kenneth gonsalves wrote:
> On Tue, 2012-01-24 at 18:33 -0600, James Bennett wrote:
> > On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.smi...@gmail.com> wrote:
> > > what does the u'%s %s' % mean... I cannot find any exaplanation of
> > > this in the docs?
> > It's not covered in Django's documentation because it's a standard
> > feature of the Python programming language -- this is basic Python
> > string formatting, which follows similar conventions to printf()-style
> > formatting in other languages.
> soon to be deprecated in favour of using format.
> --
> regards
> Kenneth Gonsalves
> "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?
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 Operations http://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:
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:
... 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.
> > "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?
> 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:
> 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:
> ... 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.
>is the two spaces between each {} two seperate the individual strings >with two spaces?
It is up to you. The purpose of the __unicode__() method is to return nice and human readable Unicode string that represents the object. It is used for example in the admin mode that was designed to be general for whatever model. Whenever the admin template needs to display textual representation of the whole record as a string, it simply calls the function for getting a string representation of the whole object. The function calls the __unicode__() method that is implemented by you. Whatever you want to see in the case should be created by your definition of the method. Try to put there strings that you can be sure they are yours -- just to see what the method is called for.
Also, you are not forced to display all the parts of the record. It is only for the display purpose. It should be just a string that represents the record. You probably want to put something meaningful there.
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:
JohnA wrote:
> 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: