Earlier I was making some code and when I made a typo while making the variable myvar, it did not through an error or act differently. Here is the line:
myvar, var2 = 1000, 1000,
Why is there not an error?
Earlier I was making some code and when I made a typo while making the variable myvar, it did not through an error or act differently. Here is the line:
myvar, var2 = 1000, 1000,
Why is there not an error?
With the new example, you're actually still dealing with tuples. If you print out what the right hand side is, you'll get
testVar = 1000, 1000,
print(testVar)
# result:
(1000,1000)
What is actually happening under the hood is that Python sees a tuple, then unpacks it into two values and assigns one to myvar
and the other to var2
. At the end of the day, the right hand side still acts as a tuple.
In fact, another way we know this to be true is if we try to unpack it but we do not provide enough variables:
myvar, = 1000, 1000,
This throws an exception:
ValueError: too many values to unpack (expected 1)
You are generating a tuple and unpack it at the same time into two different variables. You could check with
mytuple = 1000, 1000,
This will generate a tuple
of (1000,1000)
whether the comma in the end is there or not (there's no value following).
You'd generate a tuple of size 1 with
mytuple = 1000,
Now, you're effectively destructing the tuple in the next step with
myvar, var2 = 1000, 1000,
In the end you have two variables of type int.
You have edited your post, so now it asks a different question.
In [1]: myvar = 10, 10
In [2]: myvar
Out[2]: (10, 10)
In [3]: myvar[0]
Out[3]: 10
In [4]: myvar, myvar2 = 10, 10
In [5]: myvar
Out[5]: 10
In [6]: myvar2
Out[6]: 10
In [7]: myvar, myvar2 = [10, 100] # A list
In [8]: myvar
Out[8]: 10
In [9]: myvar2
Out[9]: 100