-8

I constantly ask this to myself when reading/writing Python related questions or answers, so I'll use the Python console as example, but I'm pretty sure it can be extended to other contexts.

See for example my answer here, the code part is:

>>> from functools import reduce
>>> d = {1: pd.Timedelta('0 days 01:44:39'),
 2: pd.Timedelta('0 days 02:34:01'),
 3: pd.Timedelta('0 days 00:49:15'),
 4: pd.Timedelta('0 days 01:13:26'),
 5: pd.Timedelta('0 days 00:38:14')}
>>> reduce(lambda a,b: a+b, d.values())
#Timedelta('0 days 06:59:35')

I use >>> for denoting a console entry, so one can easily distinguish different entries. Nevertheless, if someone wants to test it, it would be unavoidable to copy line by line because copying the whole code means to copy the >>> 's too, which will lead to an error if pasted in the Python console.

Then, my question is, is there a way to make the >>> not selectable when copying, in order to make testing easy?

I've seen something like this a few times in other websites like here, where you can copy the example code omitting >>> and ... .

I'm also open to the idea that this is not necessary since one could just omit >>> , but I'm not convinced at all because in some contexts it could be better to explicitly show a console-formatted code. Also, it would make easier to copy from console to paste it in SO.

Something similar is discussed here, but it is focused on Python console conventions instead of a way to make text not selectable and, as I mentioned before, it is not necessarily a Python topic only.

9
  • 8
    Stack Overflow is not optimized for copy-pasta. Why do you need to include the >>> at all? Shouldn't the description provided in your body text make it clear what the context of the code block is? Feb 9, 2021 at 22:56
  • 6
    @CodyGray with the >>> one can differentiate console inputs and outputs. Outputs are great to know if one if doing something correctly. Also, as I said in my post, it would be easy to copy all from console instead of copying line by line, which can be really tedious in some cases.
    – Pablo C
    Feb 9, 2021 at 23:00
  • 8
    What's the problem with using comments for outputs? Syntax highlighting makes it pretty clear what's a comment (and hence output) and what's not, plus they can be copied fine without impacting the way the code runs Feb 9, 2021 at 23:06
  • 1
    @Nick it can be tedious too. There are some cases in which the console output is a large text (an error for example), if you copy it from the console you'll have to add # (or whatever that denotes a comment) at the beginning of every line
    – Pablo C
    Feb 9, 2021 at 23:17
  • Maybe try including the >>> outside the code box? Maybe that wouldn't work. I agree that this is a problem.
    – 10 Rep
    Feb 10, 2021 at 0:03
  • 4
    Can't you simply include another code block saying "for convenience, this is the code that can be pasted into the terminal"? Because other people might want to copy the output/error messages too.
    – user202729
    Feb 10, 2021 at 2:50
  • Can't say it's a terrible feature request, but it can be confusing to users (see above) and there are more important things that never gets implemented.Ed
    – user202729
    Feb 10, 2021 at 2:51
  • 2
    if you copy it from the console you'll have to add # (...) at the beginning of every line That's easy. Paste it in an editor that is capable of regular expressions and replace ^ with #. Feb 10, 2021 at 6:51
  • 2
    Be aware that various Python shells and IDEs understand the >>> notation. For example, the IPython shell has no problem running the code snippet as-is. Feb 10, 2021 at 9:00

1 Answer 1

5

Whether such feature request will actually be implemented is hard to say.

To give a more immediate solution: I usually use console-like formatted code for one/two lines of code demonstrating something simple and/or small. Anything longer will go to a copy-paste-able code block, and a separate code block (maybe with `lang-none`) for the outputs (errors included).

So the way I would format your answer would be something like:

You can useĀ functools.reduce:

from functools import reduce
import pandas as pd

d = {1: pd.Timedelta('0 days 01:44:39'),
        pd.Timedelta('0 days 02:34:01'),
        pd.Timedelta('0 days 00:49:15'),
        pd.Timedelta('0 days 01:13:26'),
        pd.Timedelta('0 days 00:38:14')}

print(reduce(lambda a,b: a+b, d.values()))

Which will give:

'days 06:59:35'

This is both easily copy-pasted and run, and also gives a clear indication of the output without the need to actually run the code.

Using the >>> notation is also problematic because you have lines of code and output mixed together making it harder to copy-paste, even putting aside those >>>. As I said, I would only use it for very basic demonstrations. To take example in your case:

You can useĀ functools.reduce:

>>> reduce(lambda x,y: x+y, [1, 2, 3])
6

This is still easily copy-pasted and gives the OP a good idea of how to move forward.


Lastly, to demonstrate why this type of writing should be avoided, even if such copying functionality was available, I will use a mock example.

Console-like:

>>> a = 6
>>> a+4
10
>>> a+3
9

File-like:

a = 6
print(a+4)
print(a+3)

Gives:

10
9

Again, the first one requires to remove the >>> and the outputs when copy-pasting. The second one is copy-pasted as-is, while still keeping a clear view of the output in the post itself.

4
  • 1
    Yes, I agree; I prefer this presentation anyway, even if the tooling had first-class support for that shown in the question. Feb 10, 2021 at 15:51
  • I agree, now I'm convinced. What do you think about code that is already posted as in my answer (with the >>> or something equivalent)? should it be edited?
    – Pablo C
    Feb 11, 2021 at 4:03
  • 1
    @PabloC I wouldn't go on a specific rampage for editing those out, it also might conflict with the author's intent so I would avoid such edits in general.
    – Tomerikoo
    Feb 11, 2021 at 7:00
  • @PabloC Here is a recent example of a reasonable use of that formatting: stackoverflow.com/a/66151677/6045800
    – Tomerikoo
    Feb 11, 2021 at 9:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .