Skip to content

Instantly share code, notes, and snippets.

@MartinSeeler
Created January 21, 2021 07:35
Show Gist options
  • Save MartinSeeler/4937f8b8dc99d7ba570e0a0d98118813 to your computer and use it in GitHub Desktop.
Save MartinSeeler/4937f8b8dc99d7ba570e0a0d98118813 to your computer and use it in GitHub Desktop.
Python ES6 Spread operator
import inspect
def get_var_name(var):
callers_local_vars = inspect.currentframe().f_back.f_back.f_locals.items()
return [var_name for var_name, var_val in callers_local_vars if var_val is var][0]
def spread(*xs):
return dict(zip(map(get_var_name, xs), xs))
foo = "Hello"
bar = 1337
baz = [1,2,3]
print(spread(foo, bar, baz))
# prints {'foo': 'Hello', 'bar': 1337, 'baz': [1, 2, 3]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment