-17

I edited this answer on Stack Overflow and improved its format, so it's easier to read and conforms to Python best style/format practices. Is it a good edit or not?

It used to look like this

return HttpResponseRedirect('/folder/'+str(folder_id)+'/')

I changed it to this

return HttpResponseRedirect(f'/folder/{folder_id}/')
3
  • 2
    In other words, converting Python code (on Stack Overflow) to use f-strings. Commented Sep 20, 2022 at 9:33
  • 4
    See When should I make edits to code? Commented Sep 20, 2022 at 10:48
  • The PEP you linked to only proposes the f-string language feature, it is silent on the matter of best practice for string formatting, as is PEP-8, the style guide for code in Python's standard library. (To be fair, PEP-8 implies that s1 + s2 + s3 is poor style, but it recommends str.join rather than string formatting as the alternative). Commented Sep 20, 2022 at 20:38

2 Answers 2

36

The trouble is you've changed the answer from one that works in any version of Python to one that now requires at least Python 3.6. Anyone having to work with a legacy version of Python will now find that the answer doesn't work for them, worse they won't necessarily find an answer that does.

That's always one of the dangers of changing code in an answer, the other is that you inadvertently introduce some typo and break it altogether of course.

You should rollback the edit you've made to the existing answer. If you really feel strongly that you have a better answer then you could always add it as a new answer. You can even explain in your answer that it's only for Python 3.6+

8
  • 7
    I generally agree with the answer but not the "So just write your own answer" point (at least in this case). We really don't need another answer doing the same thing with just a different way of string formatting (Plus string formatting wasn't even the point of the question). It's like posting an answer saying: "The other answer was good but it doesn't follow proper naming conventions here's the same code with variable names that follow PEP-8" Commented Sep 20, 2022 at 11:21
  • 3
    @AbdulAzizBarkat fine, then answer this question with your suggestion as to what should happen. Commented Sep 20, 2022 at 12:20
  • I've edited When should I make edits to code? to give my suggestion Commented Sep 20, 2022 at 12:57
  • 5
    @AbdulAzizBarkat that's the FAQ, you really should seek agreement before editing it. Commented Sep 20, 2022 at 13:05
  • 1
    I think it's a little absurd that "a non-legacy version is required" isn't assumed for contemporary edits (i.e., unless the question specifically asked about a legacy version). The entire point of having a release schedule for versions that includes deprecation and sunsetting, is so that we can limit how much we have to worry about older, inferior ways of doing things. Commented Sep 20, 2022 at 13:50
  • @KarlKnechtel I mean, that's exactly how it works, from the perspective of the people building the versioned thing. They can simply say "Well that's an old version we no longer support, good luck", but they can't get everyone who has a stable release of something on an older version to create a new release on the new version. Here on SO, we're a service to all developers, not just the developers of said versioned thing, many of whom may have very good (to them) reasons for continuing to use an older version. It would be destructive to comb through SO and update every answer to 3.6.
    – Kevin B
    Commented Sep 20, 2022 at 14:47
  • 2
    First off, 3.6 is also EOL now. Second, what about answers that stop working in new versions? (Yes, I know Python is following semantic versioning, but we did have one such major kerfuffle already.) Commented Sep 20, 2022 at 14:52
  • w.r.t. the argument that this breaks backwards compatibility: Many "new" answers since Python 3.6 (2016) will use f"{}". Should all new answers be edited to use a Python <3.6 compatible string? I think it's more important to teach modern styles in answers since most people use Python 3.6+. Besides, the backwards conversion is rather trivial after someone googles "funny looking f in front of string". Commented Sep 30, 2022 at 1:45
-23

Yes, this is an appropriate edit.

Questions and answers on Stack Overflow naturally come with some fluff to establish the context of the specific situation. However, these are not literally relevant for the general case; other people finding the Q&A for their similar problem have to adjust these parts anyway.

Editing these parts does not change the content relevant for the Q&A. There is usually no loss of primary information when adjusting these incidental examples.1
Discussing every auxiliary details would be distracting, so ideally they conform to best practice. In the interest of maintaining a well-curated knowledge base, it is justified to adjust such examples when they are out of date.

When should I edit posts?

[…] Common reasons for edits include:

  • To correct minor mistakes or add updates as the post ages

1 Keep in mind that consistency of the entire Q&A may be relevant, though. Comparing question and answer code should reveal the relevant changes, not auxiliary improvements. Consider to adjust both the answer and question so that minor improvements do not draw undue attention.

2
  • 2
    There is a time to move forward (Python 3.6 was released at the end of 2016), but some are still using Turbo C++. And a default version of Python 3 on Ubuntu 16.04 (that supports 32-bit hardware) is 3.5.2. Commented Sep 20, 2022 at 12:58
  • 1
    @PeterMortensen Well, yes these dinosaurs exist – I am one of the folks using such old versions. But they will always exist. There will never be that perfect time when we can move forward without leaving anyone behind. Python 3.5 is EOL. The time to move forward was two years ago. Commented Sep 20, 2022 at 13:59

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