Is there a convention for writing Python when deprived of newlines and whitespace? For example, stackexchange comments, Twitter, text messages...

  • 1
    how would one write "for i in range(10):" without white spaces "foriinrange(10):" is this what you asked?
    – nischayn22
    Commented Jun 28, 2012 at 6:56
  • Screenshots of code written in an actual editor :) Commented Jun 28, 2012 at 7:51
  • nischayn22, no, how would you write a multi-line snippet in one line? Commented Jun 28, 2012 at 7:54
  • If it is a comment to an answer you may be able to edit the answer. Or even edit the question.s Commented Jun 28, 2012 at 11:17
  • 1
    Whatever you do, dont start using curly braces. People will flip out.
    – ankit
    Commented Jun 29, 2012 at 15:20

4 Answers 4


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/


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.


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.


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

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.

  • 1
    After reading others' posts I think I misunderstood the question.
    – DevPlayer
    Commented Jul 3, 2012 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.