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.
-
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
S.Lott : No Statement can be ()'d Only expressions. -
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
Richard Levasseur : Does this include the default if-else? a = b if c else d?Andrew Dalke : 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. -
Here's an exact answer:
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.