views:

279

answers:

3

I was intrigued by this answer to my question about getting vim to highlight unmatched brackets in python code. Specifically, I'm talking about the second part of his answer where he mentions that the C syntax highlighting is actually flagging as an error any instance of curly braces inside parens. It is an unobtrusive cue that you have unclosed parens when all of your downstream curly braces light up in red.

That trick works because C syntax doesn't allow curly braces inside parentheses. To satisfy my (morbid?) curiosity, can I do something similar with python code? Is there anything in python syntax that isn't legal inside parentheses?

Note: I'm not trolling for a better answer to my other question (there are plenty of good answers there already). I'm merely curious if this trick is even possible with python code.

A: 

I'm not sure what are you trying to do, but how about "def" or "class"?

this snippet is valid when it's not inside parenthesis

class dummy: pass
hasen j
No Statement can be ()'d Only expressions.
S.Lott
+5  A: 

Any Python statement (import, if, for, while, def, class etc.) cannot be in the parentheses:

In [1]: (import sys)
------------------------------------------------------------
File "<ipython console>", line 1
  (import sys)
       ^
<type 'exceptions.SyntaxError'>: invalid syntax
bialix
Does this include the default if-else? a = b if c else d?
Richard Levasseur
the "b if c else d" can be in ()s because it's an expression. The assignment part ("a = ...") cannot, because in Python assignment is a statement. In Python, no statement can be in parentheses.
Andrew Dalke