0

Why do these two blocks of code not do the same thing?

category = None
prodid_brand = None
prod_type = None
prod_application = None
prod_handletype = None
prod_series = None

I wanted to "clean up" my code by doing the following, but it does not work the same as the code above.

column_list = [category, prodid_brand, prod_type, prod_application, 
               prod_handletype, prod_series]

for col in column_list:
    col = None

Also is there a "cleaner" way to instantiate all the variables than the top block of code.

3
  • "but it does not work the same as the code above." How? Commented May 16, 2017 at 21:53
  • 4
    I doubt it works at all, you've got a list full of NameErrors. You should read nedbatchelder.com/text/names.html; it's not clear why you thought they would do the same thing. Commented May 16, 2017 at 21:53
  • category = prodid_brand = prod_type = prod_application = prod_handletype = prod_series = None Commented May 16, 2017 at 21:54

5 Answers 5

6

You can chain the assignments like:

category = prodid_brand = prod_type = None
prod_application = prod_handletype = prod_series = None
Sign up to request clarification or add additional context in comments.

Comments

3

The other answers are great ways to more cleanly/efficiently set all the variables to None.

However, to answer this question:

Why do these two blocks of code not do the same thing?

The reason is, with your first line

column_list = [category, prodid_brand, prod_type, prod_application, 
               prod_handletype, prod_series]

you're actually setting the variable at each index in the list equal to each of those variables. So, column_list[0] = category, column_list[1] = prodid_brand, etc.

Then with the next lines

for col in column_list:
    col = None

you just changing the variable at each of those list indexes, and setting them to None (equivalent to column_list[0] = None).

Hence why none of your initial variables (category, prodid_brand, etc) are getting set, and you're ending up with a list of six None values instead.

2 Comments

You're not changing the list indexes. The list is not changed. You're merely changing the variable col, which has no knowledge of any list elements.
@kindall good point. I updated my answer to be more accurate.
2

You can assign multiple variables to the same object

var1 = var2 = None

Comments

0

Your question "Why do these two blocks of code not do the same thing?"

Your first block of code is assigning None to variables - aka initializing them

Your second block of code is generating a variable as it iterates through the list column_list - assigning a value to the variable col in your for loop does actually make col evaluate to None but only in that loop (aka it isn't assigning it back to the original list)

If you want to assign variables in the loop -

column_list = [category, prodid_brand, prod_type, prod_application, 
           prod_handletype, prod_series]

for index,value in enumerate(column_list):
    column_list[index] = None   

but this will infact give you a list of None's

column_list = [None,None,None,None,None,None]
not
column_list = [category=None, prodid_brand=None, prod_type=None, prod_application=None, prod_handletype=None, prod_series=None]

Looks like maybe you would be better served by a dictionary than a list.

Comments

0

I use this:

a, b, c, d = (None,) * 4
f, g, h, i = (False,) * 4
print(a, b, c, d, f, g, h, i)

output:

None None None None False False False False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.