Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

From their examples:

  projects.insert({
      '_id':   "BedquiltDB",
      'description': "A ghastly hack.",
      'quality': "pre-alpha",
      'tags': ["json", "postgres", "api"]
  })
Does anyone actually mix quotes like this in python? It's horrible.


It depends.

It's useful to be able to contain quotes in other quotes without escaping them.

i.e. '"hello there"' or "it's nice to see you"

Also, on many projects I've worked on, single quotes were for strings that were not meant to be shown to users (keys etc), and double quotes were for strings that would eventually be shown on a screen. It looks like that's what they're doing here.


> Also, on many projects I've worked on, single quotes were for strings that were not meant to be shown to users (keys etc), and double quotes were for strings that would eventually be shown on a screen. It looks like that's what they're doing here.

Interesting, I've never seen that convention before.


Not sure where it comes from but I do the same. Single for most things like atoms etc and double for text (user facing / error messages / logging).


Well, I do it too. It's just for convenience, you use the apostroph character much more frequently in ordinary language.


I have no idea why I'd do that in Python.

In Ruby, I use single-quoted strings for things that can't possibly, by their nature, contain any string interpolation, to indicate that this is so; and then double-quoted strings for more arbitrary text that either contains interpolation, or might, in the future, get interpolation added. The coder here might be used to Ruby programming.

Though, also, it occurs to me that—since this is a dictionary that will become JSON—its type is {String => Object}. The single-quoted strings sort of read to me as indicating strings that couldn't be any other type; while the double-quoted strings are strings just because strings happen to be the response there, but could be any other object.


Huh, good catch. I guess I default to using single-quotes for dictionary keys, and double quotes for text strings


I do this too, single quotes for simple/static strings, double quotes for (potentially) complex/dynamic strings. Nothing wrong with that as long as you are consistent, though I suppose it's a very subjective thing.


> It's horrible.

i can dig it. works decently as visual articulation.


Single-quotes is the typical way to go. I do know when I'm not linting that I mix and match unintentionally. Easy to forget


I wish this was defined by pep8, but sort of glad it's not.

Much python code uses single quotes, with double quotes for doc strings. Except where it is more convenient.

More convenient places include structures when escaping strings. "Joe's easier here's why." Is better than 'Joe\'s easier here\'s why.' Also structures which are JSON structures too (makes copy/pasta easier).


Another commenter articulated this, but my general style was single quotes on 'atoms' (e.g. mostly dictionary keys or set values), double quotes on anything intended to be read by a person. For that reason: to avoid \' all over.

Though with py3, using alt-shift-] on my machine gives me ’, so I can do 'Joe’s easier, here’s why.'


Or py2 via # -- coding: utf-8 --




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: