Anvil.server.call() returning tuple (multiple values)

I just tried returning multiple values from an @anvil.server.callable function. E.g.,

header, nodelist = anvil.server.call('fetch_outline', 'outline_name')

As I understand it, this means that my function (fetch_outline) is returning a Python tuple.

This actually works. However, “tuple” is not listed as one of the valid data types at https://anvil.works/doc/#-div-id-server_module_restrictions-restrictions-div-

Should I be returning a list instead? Or should “tuple” be added to the list of valid data types?

In this case isn’t it essentially just a list?

Not quite. A list is mutable, and so it can be extended without losing its identity as an object. A tuple is immutable, so it can be used as a dictionary key.

In this case, I could return either type, because both types are iterable, and so will be unpacked by the assignment. The question really is about support.

  1. If tuples are fully supported, then that makes a significant improvement to the kinds of data that can be transferred in an Anvil server call, and developers would benefit from knowing about it.
  2. If my result is a fluke, then I had best not rely on it, and others should be warned to stay away from it.
  3. If all iterables are being transported as lists (or dicts), then that, too, would be good to know.

Returning multiple values, as tuples, is fairly common in Python code.

It looks like it’s 3.

This:

@anvil.server.callable
def test_tuple():
  return (1,2,3)

plus this:

  def button_1_click (self, **event_args):
    print(anvil.server.call('test_tuple'))

equals this:

[1, 2, 3]
1 Like

Yes, tuples get turned into lists on the way to or from server function. They’re still iterable, though, so you can still do:

x, y, z = anvil.server.call(...)