Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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

share|improve this question
how would one write "for i in range(10):" without white spaces "foriinrange(10):" is this what you asked? –  nischayn22 Jun 28 '12 at 6:56
Screenshots of code written in an actual editor :) –  Joris Timmermans Jun 28 '12 at 7:51
nischayn22, no, how would you write a multi-line snippet in one line? –  Steve Bennett Jun 28 '12 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 –  mhoran_psprep Jun 28 '12 at 11:17
Whatever you do, dont start using curly braces. People will flip out. –  Ankit Soni Jun 29 '12 at 15:20

3 Answers 3

up vote 5 down vote accepted

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/

share|improve this answer

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.

share|improve this answer
After reading others' posts I think I misunderstood the question. –  DevPlayer Jul 3 '12 at 16:29

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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