What is this syntax ?

L

Laurent Claessens

Le 19/06/2011 15:41, candide a écrit :
With Python 2.7 :



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
"

Laurent
 
C

Chris Angelico

With Python 2.7 :

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:
'"Ha ha! \'Tis mine!", he said.'

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

Chris Angelico
 
C

candide

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

or with string formatting :
 
R

Roy Smith

candide said:
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


or with string formatting :

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 :)
 
R

Roy Smith

rusi said:
You dont find


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.
 
V

Vito 'ZeD' De Tullio

Roy said:
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
b

and you can achieve php interpolation via locals()
b
 
S

Steven D'Aprano

well, in python3 you can use dict to format strings

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:
'spam'


and you can achieve php interpolation via 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
 
V

Vito 'ZeD' De Tullio

Steven said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top