Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Unpacking tuples as parameters?

Hi Team,

When I want to use hero.moveXY(*sometuple) it does not appear to unpack the tuple and I still have to provide both positional arguments. This kind of adds extra work, and x & y are a logical pair so ideally could use together.

This forced me to call hero.moveXY(sometuple[0], sometuple[1])
Could we add support for unpacking via *?

Error message in the game:
ArgumentError: moveXY takes only undefined argument. should have type number, but got null.
moveXY requires 2 numbers as arguments, x and y.

Hi @AlyTomji, welcome to the community :partying_face:!
I think I can help your, but I don’t really understand the what the “sometuple” means. Like double, triple, quadruple, or as a variable? I don’t think “double” is a square or something like that? Sorry, I don’t really understand what “sometuple” means (I’m only 6th Primary). Let me try:
hero.moveXY(x, y) takes in two variables, which of both should be numbers. If you type hero.moveXY(quintuple, triple); it won’t register as quintuple and triple are both tuples, not numbers. And also, hero.moveXY(x, y); requires two variables, so if you only put one, even if we can make it a variable and dissect it into somethingX, somethingY, you actually can’t because 34 is not a valid identifier. And also if you are talking about graphs, I don’t think Javascript can dissect it. This example is xy - 5 = 0:
Delete me
You said that because the asterisk can’t unpack a variable, but how do you unpack a tuple? Also, tuples aren’t graphs (according to my knowledge (which might be wrong, I dunno)), so the asterisk can’t dissect it even if it can. You can use a hero.move(targetPosition); to move the hero if you want to use a single argument, but if you don’t want the hero to move for only a short distance, use a while loop.
Hopefully, this helped! — Ethan

1 Like

do you really ask for this:

def move2(pos):
    hero.moveXY(pos.x, pos.y)
hero.move2 = move2      # CoCo python
# hero["move2"] = move2 # real python.

# calling the "method"      # call in real python
hero.move2({'x':8, 'y':56}) # hero["move2"]({'x':8, 'y':56})
hero.move2(Vector(86, 50))  # hero["move2"](Vector(86, 50))

Here is a link that explains it better:
Look at the title " Unpack elements in list or tuple to function arguments using *"
it’s somewhat common practice, but usually more so for named arguments via **kwargs

1 Like

Also, you can’t unpack graphs as there are multiple answers (actually infinite)