I'm often providing example code for my answers, which I tend to test in IDLE.
I usually create a new file with IDLE, writing the code that is required, and then hit F5 to execute that file in the Python interpreter.
I then get the output on the interpreter, so I can put proof that the code is giving the correct result in the answer. However the >>>
is very annoying when trying to copy from the answer.
There are a couple of alternatives to put the example code in an answer:
Write the same code twice, once as it is in the file, once prefixed with
>>>
to indicate running the code:class Kolo(Figura): def __init__(self, nazwa, promien): super().__init__(nazwa) self.promien = promien def calculate(self): super().calculate() print(2 * 3.1415 * self.promien) And then write **DEMO:** >>> class Figura(object): ... def __init__(self,nazwa): ... self.nazwa = nazwa ... def calculate(self): ... print(self.nazwa) ... >>> class Kolo(Figura): ... def __init__(self, nazwa, promien): ... super().__init__(nazwa) ... self.promien = promien ... def calculate(self): ... super().calculate() ... print(2 * 3.1415 * self.promien) ... >>> kolo1 = Kolo('kolo',4) >>> kolo1.calculate() kolo 25.132
A mixed style:
class Kolo(Figura): def __init__(self, nazwa, promien): super().__init__(nazwa) self.promien = promien def calculate(self): super().calculate() print(2 * 3.1415 * self.promien) >>> >>> kolo1 = Kolo('kolo',4) >>> kolo1.calculate() kolo 25.132
Only providing prefixed code:
>>> class Figura(object): ... def __init__(self,nazwa): ... self.nazwa = nazwa ... def calculate(self): ... print(self.nazwa) ... >>> class Kolo(Figura): ... def __init__(self, nazwa, promien): ... super().__init__(nazwa) ... self.promien = promien ... def calculate(self): ... super().calculate() ... print(2 * 3.1415 * self.promien) ... >>> kolo1 = Kolo('kolo',4) >>> kolo1.calculate() kolo 25.132
What is the preferred way of representing this on Stack Overflow?
I feel that 1) is very verbose and often a chore to type up while 3) is a pain in the a%% to use for the author, as copypasting will involve some s//
post processing.
I often tend to use 2), I but don't see this often from other users.
>>>
and...
make it harder for others to copy your code. I'd use the "mixed" style and add something like "Example:" (or "Demo:") before the actual demo.>>>
manually even though they are not from the interpreter. It serves two purposes. 1. It helps easily understand as to what is the input and what is the output. 2. (Call me sadistic) When the users manually remove the>>>
line by line they do a bit of work. In this way they actually go through the code instead of blindly copying and pasting. The answerer would have spent a lot of his valuable time in writing the answer, why is it a pain in the a%% for the OP to remove 4 characters per line. (Or has the world become too lazy ;) )