-1

Here, PyCharm warns me for using b outside of the while loop because, and I agree, if we don't go through the loop, b will not be defined.

enter image description here

What is the best practice here, adding # noqa after b["test"] = 8, declaring b = {} before the while loop (but then we are re-declaring it to be b = {} the first time we go through the loop)? I'd tend to the second solution because it makes the code more readable, but maybe there's a better solution?

Code:

a = 0
while a != 10:
    b = {}
    a += 1
    b[a] = "something"
b["test"] = 8

  • My minimal reproducible example was indeed not the most relevant, I've updated it to be in a way that re-declaring it `b` each time is necessary. Sorry. – FluidMechanics Potential Flows May 11 '22 at 22:37
  • 2
    It's still not really obvious why re-defining `b` inside the loop is necessary, but regardless, you would do both - define `b` before the loop, and again in the loop. Rule of thumb: If I need `b` to exist after the loop, regardless of whether or not the loop runs, then I should define it before the loop. – Paul M. May 11 '22 at 22:53
  • Ok! So, the conclusion I gathered is: it's not bad practice to define `b` before even though we know it will be declared twice. – FluidMechanics Potential Flows May 12 '22 at 11:40
  • There are no declarations in python. Only definition. You define `b` outside the loop to prevent an error if the loop isn't entered. You re-define `b` inside the loop with whatever value you want it to have. Note that in your case you only really define the value of the dict at the key `a`, i.e. `b[a]` in the loop. If you _re-define `b` to be an empty dict in every iteration of the loop_, it will have only one key -- the one you set in the last iteration -- when your loop ends. In other words, your code is not the same as simply moving the definition of `b`outside the loop (run it and see why) – Pranav Hosangadi May 12 '22 at 15:38
  • No yes I get that, my code doesn't make sense because it's a minimal reproducible example. My question was: is it bad practice to do b = {} and then inside the loop b = {} because I want to "reset" b at each iteration of the loop. and use b after the loop. Even though I KNOW for a fact the loop will be run through at least once. – FluidMechanics Potential Flows May 12 '22 at 15:52

1 Answers1

0

It seems that is an example code but it has no sense to redeclare b each time. The best solution is to declare it before the while (it's a mandatory practice in languages as C/C++). So, the code will be:

a = 0    
b = {}
while a != 10:
    a += 1
    b["something"] = a
b["test"] = 8
Palinuro
  • 184
  • 1
  • 1
  • 15
  • My minimal reproducible example was indeed not the most relevant, I've updated it to be in a way that re-declaring it `b` each time is necessary. My bad. – FluidMechanics Potential Flows May 11 '22 at 22:37
  • 1
    Ok, in such a case the best practice would be declaring `b` as if the execution doesn't enter to the `while`. That is, you have to question yourself what is the default value of `b` in the case of `not a != 10`? – Palinuro May 11 '22 at 22:56
  • Ok thank you! The conclusion I gathered is: it's not bad practice to define `b` before even though we know it will be declared twice. – FluidMechanics Potential Flows May 12 '22 at 11:40