Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 344
Rivadiz Avatar asked Oct 25 '15 15:10

Rivadiz


People also ask

How do you initialize the same value for multiple variables in Python?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

Can you initialize multiple variables in the same line?

You can declare multiple variables in a single line.

How do you initialize multiple variables with the same value in Java?

int a, b, c; You can also assign multiple variables to one value: a = b = c = 5; This code will set c to 5 and then set b to the value of c and finally a to the value of b .


1 Answers

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. 
like image 156
Shan Avatar answered Sep 29 '22 20:09

Shan