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.
>>>
at all? Shouldn't the description provided in your body text make it clear what the context of the code block is?>>>
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.#
(or whatever that denotes a comment) at the beginning of every line>>>
outside the code box? Maybe that wouldn't work. I agree that this is a problem.#
(...) at the beginning of every line That's easy. Paste it in an editor that is capable of regular expressions and replace^
with#
.>>>
notation. For example, the IPython shell has no problem running the code snippet as-is.