3

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

Steve Bennett
  • 3,419
  • 4
  • 20
  • 24

4 Answers4

5

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/

Morothar
  • 211
  • 1
  • 2
3

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.

goodside
  • 131
  • 2
1

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.

tylerl
  • 4,850
  • 21
  • 32
0

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.

DevPlayer
  • 216
  • 1
  • 4