Is there a convention for writing Python when deprived of newlines and whitespace? For example, stackexchange comments, Twitter, text messages...
-
1how would one write "for i in range(10):" without white spaces "foriinrange(10):" is this what you asked? – nischayn22 Jun 28 '12 at 06:56
-
Screenshots of code written in an actual editor :) – Joris Timmermans Jun 28 '12 at 07:51
-
nischayn22, no, how would you write a multi-line snippet in one line? – Steve Bennett Jun 28 '12 at 07:54
-
If it is a comment to an answer you may be able to edit the answer. Or even edit the question.s – mhoran_psprep Jun 28 '12 at 11:17
-
1Whatever you do, dont start using curly braces. People will _flip_ out. – ankit Jun 29 '12 at 15:20
4 Answers
Short answer: No, there is no such convention.
Typical solution would be to use a pastebin service and link to you snippet there.
There are several services for that. You might want check this link: http://www.1stwebdesigner.com/freebies/paste-share-code-snippets/

- 211
- 1
- 2
I use the unicode RETURN SYMBOL ⏎ (U+23CE) to represent newlines. It's rare that indent level would not be clear from context, but when necessary you could represent tabs explicitly with unicode RIGHTWARDS ARROW TO BAR ⇥ (U+21E5).
Of course, neither of these will be interpreted by Python. They're purely for human eyes.

- 131
- 2
Python allows you to use semicolons to separate statements. As for control structure, that's a bit more difficult. Some projects use the pass
keyword (a no-op) to indicate the end of a block:
if foo==1:foo=2;pass; if foo==3:foo=4;pass;
It's not particularly readable though, and the interpreter will just mock you if you try it for real, so it's not really a general solution.

- 4,850
- 21
- 32
Is it possible in Python; mostly, you still need space characters. Is it practical? not even remotely.
There is a practice similar to a bad form of functional programming that after trying it for a while you'll say "What a waste of time." However with this style, you still need to use indentation for it to work, unless you define your functions in an eval (or some other function) string like this eval('def myfunc(obj):\n\t# do stuff\n\treturn obj\n'). Shoot yourself now.
myobject = SomeClass()
def compile(obj): # use any function name
# do stuff
return obj
def parse(obj): # use any function name
# do stuff
return obj
def transpose(obj): # use any function name
# do stuff
return obj
# here is the nonwhite space thing you want
myobject=compile(myobject).parse(myobject).tanspose(myobject)
You could have put each of those functions in a class as methods. I wanted to show that if you were to go the "I can't use newlines" things that each function call you do will have to return the object you gave it so you can chain your "operations" with a period instead of some other method.
If your functions/methods are small you could use lambdas. Something evil like: myobject=(lambda obj:obj.add(5))(myobject).(lambda obj:obj.callfunc(func,(args))(myobject)
Know that these things are not Pythonic at the very core. And you might as well do them in another programming language. I've always felt Python's main asset is readability, caused by whitespaces and lack of extraneous non-alphnumeric characters like (){}[]:;# etc.

- 216
- 1
- 4