I was looking at code in my course material and had to write a function which adds the value 99 to either a list
or tuple
. The final code looks like this:
def f(l):
print(l)
l += 99,
print(l)
f([1,2,3])
f((1,2,3))
This was used to show something different but I'm getting somewhat hung up on the line l += 99,
. What this does, is create an iterable that contains the 99 and list
as well as tuple
support the simple "addition" of such an object to create a new instance/add a new element.
What I don't really get is what exactly is created using the syntax element,
? If I do an assignment like x = 99,
the type(x)
will be tuple
but if I try run x = tuple(99)
it will fail as the 99 is not iterable. So is there:
- Some kind of intermediate iterable object created using the syntax
element,
? - Is there a special function defined that would allow the calling of
tuple
without an iterable and somehow,
is mapped to that?
Edit:
In case anyone wonders why the accepted answer is the one it is: The explanation for my second question made it. I should've been more clear with my question but that +=
is what actuallly got me confused and this answer includes information on this.