Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7956 > unrolled thread

What is this syntax ?

Started bycandide <candide@free.invalid>
First post2011-06-19 15:41 +0200
Last post2011-06-19 15:29 -0700
Articles 12 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  What is this syntax ? candide <candide@free.invalid> - 2011-06-19 15:41 +0200
    Re: What is this syntax ? Laurent Claessens <moky.math@gmail.com> - 2011-06-19 15:50 +0200
    Re: What is this syntax ? Noah Hall <enalicho@gmail.com> - 2011-06-19 14:58 +0100
    Re: What is this syntax ? Chris Angelico <rosuav@gmail.com> - 2011-06-20 00:03 +1000
    Re: What is this syntax ? candide <candide@free.invalid> - 2011-06-19 17:08 +0200
      Re: What is this syntax ? Roy Smith <roy@panix.com> - 2011-06-19 11:39 -0400
        Re: What is this syntax ? rusi <rustompmody@gmail.com> - 2011-06-19 09:58 -0700
          Re: What is this syntax ? Roy Smith <roy@panix.com> - 2011-06-19 16:20 -0400
            Re: What is this syntax ? Vito 'ZeD' De Tullio <zak.mc.kraken@libero.it> - 2011-06-19 23:06 +0200
              Re: What is this syntax ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-19 23:19 +0000
                Re: What is this syntax ? Vito 'ZeD' De Tullio <zak.mc.kraken@libero.it> - 2011-06-20 07:42 +0200
            Re: What is this syntax ? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-06-19 15:29 -0700

#7956 — What is this syntax ?

Fromcandide <candide@free.invalid>
Date2011-06-19 15:41 +0200
SubjectWhat is this syntax ?
Message-ID<4dfdfc99$0$715$426a34cc@news.free.fr>
With Python 2.7 :

 >>> x="foo"
 >>> print '"'+x+'"'
"foo"
 >>>


What is this curious syntax on line 2 ? Where is it documented ?

[toc] | [next] | [standalone]


#7957

FromLaurent Claessens <moky.math@gmail.com>
Date2011-06-19 15:50 +0200
Message-ID<4DFDFEA5.9010204@gmail.com>
In reply to#7956
Le 19/06/2011 15:41, candide a écrit :
> With Python 2.7 :
>
>   >>>  x="foo"
>   >>>  print '"'+x+'"'
> "foo"
>   >>>
>
>
> What is this curious syntax on line 2 ? Where is it documented ?

When you want to have an explicit double quote " in a string, you put in 
between single quote '.
(and vice versa)

So here you have the string
'"'
which is "
then
+x
(add x)
then
+'"'

Try also

 >>> print "'"
'
 >>> print "'"
"

Laurent

[toc] | [prev] | [next] | [standalone]


#7958

FromNoah Hall <enalicho@gmail.com>
Date2011-06-19 14:58 +0100
Message-ID<mailman.148.1308491943.1164.python-list@python.org>
In reply to#7956
On Sun, Jun 19, 2011 at 2:41 PM, candide <candide@free.invalid> wrote:
> With Python 2.7 :
>
>>>> x="foo"
>>>> print '"'+x+'"'
> "foo"

> What is this curious syntax on line 2 ? Where is it documented ?

Just to make it clear to you what is happening -

>>> x = "foo"
>>> print ' " ' + x + ' " '
 " foo "
>>>

Anyway, it's documented here -
http://docs.python.org/tutorial/introduction.html#strings

[toc] | [prev] | [next] | [standalone]


#7959

FromChris Angelico <rosuav@gmail.com>
Date2011-06-20 00:03 +1000
Message-ID<mailman.149.1308492214.1164.python-list@python.org>
In reply to#7956
On Sun, Jun 19, 2011 at 11:41 PM, candide <candide@free.invalid> wrote:
> With Python 2.7 :
>
>>>> x="foo"
>>>> print '"'+x+'"'
> "foo"
>>>>

As Laurent posted, it's simply a literal double-quote character,
enclosed in single quotes. But for making a quoted string, this isn't
reliable (what if there's a double quote in the string itself?), so
you may want to try repr() which makes a theoretically evalable
string:

>>> x="foo"
>>> print repr(x)
'foo'

>>> x=raw_input()
"Ha ha! 'Tis mine!", he said.
>>> print repr(x)
'"Ha ha! \'Tis mine!", he said.'

In this instance, repr chose to use single quotes, but the same applies.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#7967

Fromcandide <candide@free.invalid>
Date2011-06-19 17:08 +0200
Message-ID<4dfe10d1$0$28053$426a34cc@news.free.fr>
In reply to#7956
OK,  thanks for your explanation, it was just stringisation !


I erroneously focused on

+x+

as a kind of placeholder unknown to me, instead of left and right 
concatenations ;)

It would be more readable for me if it were edited

 >>> print '"' + x + '"' # better spacing
"foo"
 >>>

or with string formatting :

 >>> x="foo"
 >>> print '"%s"' %x
"foo"
 >>>

[toc] | [prev] | [next] | [standalone]


#7973

FromRoy Smith <roy@panix.com>
Date2011-06-19 11:39 -0400
Message-ID<roy-2A6DC8.11394419062011@news.panix.com>
In reply to#7967
In article <4dfe10d1$0$28053$426a34cc@news.free.fr>,
 candide <candide@free.invalid> wrote:

> OK,  thanks for your explanation, it was just stringisation !
> 
> 
> I erroneously focused on
> 
> +x+
> 
> as a kind of placeholder unknown to me, instead of left and right 
> concatenations ;)
> 
> It would be more readable for me if it were edited
> 
>  >>> print '"' + x + '"' # better spacing
> "foo"
>  >>>
> 
> or with string formatting :
> 
>  >>> x="foo"
>  >>> print '"%s"' %x
> "foo"
>  >>>

This is one of the (very) few places PHP wins over Python.  In PHP, I 
would write this as

print "'$x'"

which seems easier to read than any of the Python versions.  Of course, 
sharp-eyed readers will notice that I inverted the single and double 
quotes because

print '"$x"'

doesn't work.  If you really wanted to replicate the output exactly, you 
would have to do

print "\"$x\""

by which time you'd probably run back to writing it in Python :-)

[toc] | [prev] | [next] | [standalone]


#7985

Fromrusi <rustompmody@gmail.com>
Date2011-06-19 09:58 -0700
Message-ID<e724fc3e-8198-4fb7-b1d3-96834f3fa6bb@34g2000pru.googlegroups.com>
In reply to#7973
On Jun 19, 8:39 pm, Roy Smith <r...@panix.com> wrote:

> This is one of the (very) few places PHP wins over Python.  In PHP, I
> would write this as
>
> print "'$x'"


You dont find

>>> print '"%s"' % x

readable? Why?

[toc] | [prev] | [next] | [standalone]


#7989

FromRoy Smith <roy@panix.com>
Date2011-06-19 16:20 -0400
Message-ID<roy-DD0C35.16204819062011@news.panix.com>
In reply to#7985
In article 
<e724fc3e-8198-4fb7-b1d3-96834f3fa6bb@34g2000pru.googlegroups.com>,
 rusi <rustompmody@gmail.com> wrote:

> On Jun 19, 8:39 pm, Roy Smith <r...@panix.com> wrote:
> 
> > This is one of the (very) few places PHP wins over Python.  In PHP, I
> > would write this as
> >
> > print "'$x'"
> 
> 
> You dont find
> 
> >>> print '"%s"' % x
> 
> readable? Why?

I didn't say it wasn't readable, I said other things were easier to 
read.  There's something nice about building up strings in-line, as 
opposed to having to look somewhere to see what's being interpolated.  
To give a more complex example, consider:

print "$scheme://$host:$port/$route#$fragment"

That certainly seems easier to me to read than:

print "%s://%s:%s/%s#%s" % (scheme,
                            port,
                            host,
                            route,
                            fragment)

because I don't have to line up the nth format specifier with the nth 
data item.

[toc] | [prev] | [next] | [standalone]


#7991

FromVito 'ZeD' De Tullio <zak.mc.kraken@libero.it>
Date2011-06-19 23:06 +0200
Message-ID<mailman.164.1308517611.1164.python-list@python.org>
In reply to#7989
Roy Smith wrote:

> There's something nice about building up strings in-line, as
> opposed to having to look somewhere to see what's being interpolated.
> To give a more complex example, consider:
> 
> print "$scheme://$host:$port/$route#$fragment"
> 
> That certainly seems easier to me to read than:
> 
> print "%s://%s:%s/%s#%s" % (scheme,
>                             port,
>                             host,
>                             route,
>                             fragment)
> 
> because I don't have to line up the nth format specifier with the nth
> data item.

well, in python3 you can use dict to format strings

>>> print("%(a)s" % {'a':'b'})
b

and you can achieve php interpolation via locals()

>>> a = 'b'
>>> print("%(a)s" % locals())
b


-- 
By ZeD

[toc] | [prev] | [next] | [standalone]


#7993

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-06-19 23:19 +0000
Message-ID<4dfe841c$0$30002$c3e8da3$5496439d@news.astraweb.com>
In reply to#7991
On Sun, 19 Jun 2011 23:06:36 +0200, Vito 'ZeD' De Tullio wrote:

> well, in python3 you can use dict to format strings
> 
>>>> print("%(a)s" % {'a':'b'})
> b

It's not just Python 3. That bit of functionality goes back all the way 
to Python 1.5, which is the oldest version I have installed.

In Python 2.6 on up, you can also use the new format() method on strings:

>>> '{a}'.format(a='spam')
'spam'


> and you can achieve php interpolation via locals()
> 
>>>> a = 'b'
>>>> print("%(a)s" % locals())
> b

You can do that, but when reading code I consider any direct use of 
locals() (and globals() for that matter) to be a code smell: not 
necessarily wrong in and of itself, but unusual and suspicious enough to 
be worth a second, careful, look. As such, I would want to think long and 
hard before inflicting it on others by using it myself.

For those unfamiliar with the idea of code smells:

http://www.joelonsoftware.com/articles/Wrong.html



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#7999

FromVito 'ZeD' De Tullio <zak.mc.kraken@libero.it>
Date2011-06-20 07:42 +0200
Message-ID<mailman.169.1308548598.1164.python-list@python.org>
In reply to#7993
Steven D'Aprano wrote:

>> and you can achieve php interpolation via locals()
>> 
>>>>> a = 'b'
>>>>> print("%(a)s" % locals())
>> b
> 
> You can do that, but when reading code I consider any direct use of
> locals() (and globals() for that matter) to be a code smell:

well you're right, me neither like very much to touch locals() and (worse) 
globals(), but

1) this is the "php interpolation" Roy Smith asked for: 
       print "$scheme://$host:$port/$route#$fragment"
   where are defined scheme, host, port, route and fragment?
   or you think also this is "code smell"?

2) I'm in no way modifying the dict, just accessing in read only.

3) I'm restricting to locals() :D


btw I never used dict to format strings, so I learned how old this feature 
is :D

-- 
By ZeD

[toc] | [prev] | [next] | [standalone]


#7992

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2011-06-19 15:29 -0700
Message-ID<mailman.165.1308522593.1164.python-list@python.org>
In reply to#7989
On Sun, Jun 19, 2011 at 2:06 PM, Vito 'ZeD' De Tullio
<zak.mc.kraken@libero.it> wrote:
> Roy Smith wrote:
>
>> There's something nice about building up strings in-line, as
>> opposed to having to look somewhere to see what's being interpolated.
>> To give a more complex example, consider:
>>
>> print "$scheme://$host:$port/$route#$fragment"
>>
>> That certainly seems easier to me to read than:
>>
>> print "%s://%s:%s/%s#%s" % (scheme,
>>                             port,
>>                             host,
>>                             route,
>>                             fragment)
>>
>> because I don't have to line up the nth format specifier with the nth
>> data item.
>
> well, in python3 you can use dict to format strings
>
>>>> print("%(a)s" % {'a':'b'})
> b
>
> and you can achieve php interpolation via locals()
>
>>>> a = 'b'
>>>> print("%(a)s" % locals())
> b
>
>
> --
> By ZeD

That's a lot older than Python 3. Here's the example from the 2.3
docs:  http://docs.python.org/release/2.3/lib/typesseq-strings.html

Python 3 added a different syntax for string formatting, using .NET's
formatting syntax instead of C's.

"{scheme}://{host}:{port}/{route}#{fragment}".format(locals())

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web