pythonvariablesvariable-assignment

Python initialize multiple variables to the same initial value


I have gone through these questions,

  1. Python assigning multiple variables to same value? list behavior
    concerned with tuples, I want just variables may be a string, integer or dictionary
  2. More elegant way of declaring multiple variables at the same time
    The question has something I want to ask, but the accepted answer is much complex

so what I'm trying to achieve,

I declare variables as follows, and I want to reduce these declarations to as less line of code as possible.

details = None
product_base = None
product_identity = None
category_string = None
store_id = None
image_hash = None
image_link_mask = None
results = None
abort = False
data = {}

What is the simplest, easy to maintain ?


Solution

  • I agree with the other answers but would like to explain the important point here.

    None object is singleton object. How many times you assign None object to a variable, same object is used. So

    x = None
    y = None
    

    is equal to

    x = y = None
    

    but you should not do the same thing with any other object in python. For example,

    x = {}  # each time a dict object is created
    y = {}
    

    is not equal to

    x = y = {}  # same dict object assigned to x ,y. We should not do this.