Python Gotcha's?

R

rusi

You mean JSON expects a string with valid JSON?
Quelle surprise.

Are there languages (other than python) in which single and double
quotes are equivalent?

[No I dont claim to know all the languages out there, just that I dont
know any other language which allows single and double quotes to be
interconvertible like python does]
 
S

Steve Howell

You mean JSON expects a string with valid JSON?
Quelle surprise.

Are there languages (other than python) in which single and double
quotes are equivalent?

[No I dont claim to know all the languages out there, just that I dont
know any other language which allows single and double quotes to be
interconvertible like python does]

Python doesn't treat single quotes and double quotes in *exactly* the
same manner, because the choice of outer quotes affects whether you
need to escape the outer quote characters inside the string. But I
don't want to be overly literal--I think I know what you mean by
"equivalent" here.

JS, YAML, and HTML are pretty similar to Python with respect to single
vs. double, as far as I know/remember/care.

Perl, Ruby, and CoffeeScript have the tradition that single quotes are
interpreted literally, whereas double quotes allow for interpolation
of things within the string. This is roughly inspired by English,
where one says things like "Double quotes are an 'enclosing syntax'
for single quotes." [Yes, I'm just making that up. Sounds plausible,
right?]

Both Ruby and CoffeeScript support triple quote syntax similar to
Python.

C uses double quotes for strings, as opposed to single quotes for
characters.

Java only allows double quotes for strings.

I'll wager a guess that if you took any two programming languages
(including declarative languages like SQL/HTML) and compared how they
represented string literals, there would be at least one thing
different between them, and that difference would be a fairly
arbitrary design decision. There are probably exceptions, but
languages that have the exact same quoting rules would probably be
close dialects of each other in other respects beyond quoting.

I'll also wager a guess that at least one thing I said above was
wrong, and that's a testament to the arcane nature of representing
string literals (as well as my own lack of mental capacity for
juggling all these different rules in my brain). And that's just in
ASCII with an American English bias. Throw in Unicode--that's when
things get really confusing!

I'm happy to stand corrected on any fact above. Withhold insults,
though. I already know that string literal syntax makes me feel
stupid--no need to rub it in.
 
R

rusi

JS, YAML, and HTML are pretty similar to Python with respect to single
vs. double, as far as I know/remember/care.

[Complete ignoramus here -- writing after a few minutes of googling]

YAML: http://yaml.org/spec/current.html#single quoted style/syntax
seems to indicate that the double-quote is multi-lineable the single
not.
[So YAML double-quote is like python triple-quote]

JS:
http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript
seems to have some arcane argument about whether there is a difference
or not
 
S

Steve Howell

JS, YAML, and HTML are pretty similar to Python with respect to single
vs. double, as far as I know/remember/care.

[Complete ignoramus here -- writing after a few minutes of googling]

YAML:http://yaml.org/spec/current.html#single quoted style/syntax
seems to indicate that the double-quote is multi-lineable the single
not.
[So YAML double-quote is like python triple-quote]

JS:http://stackoverflow.com/questions/242813/when-to-use-double-or-singl....
seems to have some arcane argument about whether there is a difference
or not

I like the fact that even this seemingly harmless statement on the
stackoverflow thread invites (light-hearted) controversy:

Double quotes will wear your shift key out faster :)

The response is this:

Not on Azerty, buddy.

And, yes, I am quoting via indentation here. But quoting is not a
"gotcha". Not in the least bit. Anybody who says "quoting is a
gotcha" just doesn't understand how simple this whole quoting business
is. Quoting is completely straightforward. "Just read the specs," I
think some wise person said earlier in this thread. They're
completely correct, of course. No gotchas here. Move along...
 
M

mwilson

rusi said:
Are there languages (other than python) in which single and double
quotes are equivalent?

Kernighan and Plauger's RATFOR (a pre-processor that added some C-like
syntax to FORTRAN) did that. Published in their book _Software Tools_.

Mel.
 
R

Roy Smith

Kernighan and Plauger's RATFOR (a pre-processor that added some C-like
syntax to FORTRAN) did that. Published in their book _Software Tools_.

I used to write a lot of code in RATFOR. It was really a pretty good
tool.
 
M

mwilson

Roy said:
I used to write a lot of code in RATFOR. It was really a pretty good
tool.

ISTR that RATFOR also concatenated adjacent quoted strings to build up long
strings. That sort of puts the capstone on the two-quote-characters scheme.
I can't lay my hands on the book to prove it.

GE/Honeywell FORTRAN stole the single quote to delimit seek addresses in I/O
statements, so my RATFOR implementations had to lose the two-quote feature
and use a two-for-one scheme, like backslash-escaping and % inclusion in
moduloed strings do in Python.

Mel.
 
S

Steven D'Aprano

Are there languages (other than python) in which single and double
quotes are equivalent?

Classic REXX, CSS, JavaScript, Lua, Prolog, XPath, YAML, Modula-2, HTML,
and (of course) English. There may be others.


Other languages like Perl, PHP and Ruby support alternate delimiters with
slightly different semantics.
 
R

rusi

Classic REXX, CSS, JavaScript, Lua, Prolog, XPath, YAML, Modula-2, HTML,
and (of course) English. There may be others.

Other languages like Perl, PHP and Ruby support alternate delimiters with
slightly different semantics.

Prolog: seems to be different, see
http://stackoverflow.com/questions/4391435/how-to-manipulate-strings-in-prolog

yaml: double seems to be like python triple, ie multi-line allowed
whereas single does not allow multi-line

http://yaml.org/spec/current.html#single quoted style/syntax
 
R

rusi

Perl, first of all, has the 'q' and 'qq' operators. As much as I'd
come to dislike Perl after I discovered Python, I miss those two.
Every time I have to quote a string full of single/double quotes,
this comes to my mind:

    q{'this' is not this, but 'that' is 'that' like 'this'}
    q|'this' is not this, but 'that' is 'that' like 'this'|
    q<'this' is not this, but 'that' is 'that' like 'this'>

... with 'qq' providing the version with inerpolation. I could
always find an arbitrary character for quoting that was _not_ present
in the string, and so, most of the time, avoid quoting altogether.
It was perhaps a bit too magical, but pruced very readable strings.

Yes the q of perl is qute and qlever.
Come to think of it its very sensible because it factors delimition
from quoting just as the () and ' do for lisp.
 
A

André Malo

* Steven D'Aprano said:
KISS is a reason *for* allowing multiple string delimiters, not against
it. The simplicity which matters here are:

* the user doesn't need to memorise which delimiter is allowed, and
which is forbidden, which will be different from probably 50% of
the other languages he knows;

* the user can avoid the plague of escaping quotes inside strings
whenever he needs to embed the delimiter inside a string literal.

This is the 21st century, not 1960, and if the language designer is
worried about the trivially small extra effort of parsing ' as well as "
then he's almost certainly putting his efforts in the wrong place.

Yes, that's what you said already. My reasoning was in the part you stripped
from my quote. *shrug*

nd
 
R

rusi

Yes, that's what you said already. My reasoning was in the part you stripped
from my quote. *shrug*

Multiple symmetric quote characters breaks one of python's own zen
rules:

There should be one-- and preferably only one --obvious way to do it.
 
B

Barry W Brown

Are there languages (other than python) in which single and double
quotes are equivalent?

Fortran 90 and more recent versions.
[No I dont claim to know all the languages out there, just that I dont
know any other language which allows single and double quotes to be
interconvertible like python does]



You mean JSON expects a string with valid JSON?
Quelle surprise.

Are there languages (other than python) in which single and double
quotes are equivalent?

[No I dont claim to know all the languages out there, just that I dont
know any other language which allows single and double quotes to be
interconvertible like python does]
 
J

John Nagle

Greetings,

I'm going to give a "Python Gotcha's" talk at work.
If you have an interesting/common "Gotcha" (warts/dark corners ...) please share.

(Note that I want over http://wiki.python.org/moin/PythonWarts already).

Thanks,

A few Python "gotchas":

1. Nobody is really in charge of third party packages. In the
Perl world, there's a central repository, CPAN, and quality
control. Python's "pypi" is just a collection of links. Many
major packages are maintained by one person, and if they lose
interest, the package dies.

2. C extensions are closely tied to the exact version of CPython
you're using, and finding a properly built version may be difficult.

3. "eggs". The "distutils" system has certain assumptions built into
it about where things go, and tends to fail in obscure ways. There's
no uniform way to distribute a package.

4. The syntax for expression-IF is just weird.

5. "+" as concatenation. This leads to strange numerical
semantics, such as (1,2) + (3,4) is (1,2,3,4). But, for
"numarray" arrays, "+" does addition. What does a mixed
mode expression of a numarray and a tuple do? Guess.

5. It's really hard to tell what's messing with the
attributes of a class, since anything can store into
anything. This creates debugging problems.

6. Multiple inheritance is a mess. Especially "super".

7. Using attributes as dictionaries can backfire. The
syntax of attributes is limited. So turning XML or HTML
structures into Python objects creates problems.

8. Opening a URL can result in an unexpected prompt on
standard input if the URL has authentication. This can
stall servers.

9. Some libraries aren't thread-safe. Guess which ones.

10. Python 3 isn't upward compatible with Python 2.

John Nagle
 
R

Roy Smith

John Nagle <[email protected]> said:
1. Nobody is really in charge of third party packages. In the
Perl world, there's a central repository, CPAN, and quality
control. Python's "pypi" is just a collection of links. Many
major packages are maintained by one person, and if they lose
interest, the package dies.

While I agree that this is a problem, it's not specifically a Python
problem. There's a lot of abandonware out there. In all languages.
With both OSS and commercial products.

Having an official curated central repository is a good thing, but it
has its down side too. What happens when the curator decides not to
allow your code into the library? Down that path lies things like the
Apple Store for IOS. If Apple decides they don't want your app for
whatever reason, your app is dead.
 
M

Miki Tebeka

8. Opening a URL can result in an unexpected prompt on
standard input if the URL has authentication. This can
stall servers.
Can you give an example? I don't think anything in the standard library does that.
 
J

John Nagle

Can you give an example? I don't think anything in the standard library does that.

It's in "urllib". See

http://docs.python.org/library/urllib.html

"When performing basic authentication, a FancyURLopener instance calls
its prompt_user_passwd() method. The default implementation asks the
users for the required information on the controlling terminal. A
subclass may override this method to support more appropriate behavior
if needed."

A related "gotcha" is knowing that "urllib" sucks and you should use
"urllib2".

John Nagle
 
A

André Malo

* Grzegorz Staniak said:
Then again, practicality beats purity.

Yes.

If you ever grepped for, say, the usage of dictionary keys in a bigger
application, you might agree, that having multiple equivalent quote
characters is not as practical as it might seem in the first place.

Code is written once and read often.

nd
 
S

Stefan Schwarzer

Hi Miki,

I'm going to give a "Python Gotcha's" talk at work.
If you have an interesting/common "Gotcha" (warts/dark corners ...) please share.

(Note that I want over http://wiki.python.org/moin/PythonWarts already).

I gave a somewhat similar talk a while ago:

http://sschwarzer.com/download/robust_python_programs_europython2010.pdf

The following is a German version of the talk slides, but
covers a bit more since there was a longer time slot. Even
if you don't know German, you'll most likely understand what
I'm talking about by reading the code. :)

http://sschwarzer.com/download/robustere_python_programme_clt2010_print.pdf

Stefan
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top