I have gone through these questions,
- Python assigning multiple variables to same value? list behavior
concerned with tuples, I want just variables may be a string, integer or dictionary- 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 ?
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.
You can declare multiple variables in a single line.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With