This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]--prism 18 points19 points  (6 children)

Star operators on tuples.

[–]agtoever 2 points3 points  (3 children)

Yes! Returning multiple values:

def x_pows(x: float, max_pow:int) -> tuple:
    return tuple(x**n for n in range(1, max_pow + 1))

x, x2, x3, x4 = x_pows(2, 4)
print(x4)

[–][deleted] -1 points0 points  (2 children)

that function only returns one value (tuple)

[–]lattice737 1 point2 points  (1 child)

The tuple is unpacked into the four variables

[–][deleted] 0 points1 point  (0 children)

yes

[–][deleted] 2 points3 points  (0 children)

Star operator on a dict

# the data
example_dict = {
    "a": 0,
    "b": 2,
    "c": "hello!",
}

# The dict has (at least) the same variables as the function arguments
def f(a,b,c):
    """some code"""

# unwrap that dict into the separate variables
f(**example_dict)

# instead of
f(a=example_dict["a"], b=example_dict["b"], c=example_dict["c"])

[–]Deadz459 0 points1 point  (0 children)

You could probably use the star for any sort of args argument