Questions tagged [argument-unpacking]

Use this tag for questions related with argument unpacking, a technique that allows Arrays and Traversable objects to be extracted/unpacked into argument lists/sequences.

is usually found in PHP, Python and Go.

144 questions
4
votes
2 answers

Why does Python unpacking operator work with unordered collections

The Python 3 tutorial about the unpacking operator (*) generically speaks of a "list or tuple," while the error message for improper use says that a "sequence" is needed: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit…
brianpck
  • 8,084
  • 1
  • 22
  • 33
4
votes
2 answers

python: calling constructor from dictionary?

I'm not quite sure of the terminology here so please bear with me.... Let's say I have a constructor call like this: machineSpecificEnvironment = Environment( TI_C28_ROOT = 'C:/appl/ti/ccs/4.1.1/ccsv4/tools/compiler/c2000', JSDB =…
Jason S
  • 184,598
  • 164
  • 608
  • 970
4
votes
1 answer

Pass in argument to function by name dynamically

Is it possible to dynamically set the name of an argument being passed into a function? like this: def func(one=None, two=None, three=None, four=None): ... params = ("one","two","three","four",) for var in params: tmp = func(var=value)
Jeremy
  • 1,717
  • 5
  • 30
  • 49
4
votes
2 answers

Python style for line length and format when unpacking many return values

Suppose that function some_descriptively_named_function returns a 4-tuple of 4 return parameters. I want to call some_descriptively_named_function, adhere to the 80-character line length limit, and unpack all 4 outputs each into a…
ely
  • 74,674
  • 34
  • 147
  • 228
3
votes
4 answers

Unpack without using * (asterisk)

If I want to keep things functional, and do not want to use * in the middle, then what is the equivalent substitute function? for example, import operator as op print(op.eq(*map(str.upper, ['a', 'A']))) how do I avoid using * here? I created a…
apostofes
  • 2,959
  • 5
  • 16
  • 31
3
votes
1 answer

Can I unpack/destructure a typing.NamedTuple?

This is a simple question so I'm surprised that I can't find it asked on SO (apologies if I've missed it), and it always pops into my mind as I contemplate a refactor to replace a tuple by a NamedTuple. Can I unpack a typing.NamedTuple as arguments…
Cai
  • 1,726
  • 2
  • 15
  • 24
3
votes
3 answers

Unpacking a class

I'd like to make a class that unpacks it's objects like a dictionary. For example, with a dictionary you can do this foo = { "a" : 1 "b" : 2 } def bar(a,b): return a + b bar(**foo) outputs 3 And I'd like to be able to do this class…
financial_physician
  • 1,672
  • 1
  • 14
  • 34
3
votes
3 answers

Object unpacking assignment operation?

Is there anything like this in JavaScript? Basically, I'm looking for something along these lines: let obj_a = {test: "one", property: "two"}; let obj_b = {test: "1", other: "three"}; let obj_b ...= obj_a; // would be the equivalent of obj_b =…
3
votes
1 answer

How to disable pylint report of "E1120: No value for argument if" when passing a *args as list

I have the following code section that triggers Pylint error "E1120: No value for argument if". framework/packages/create_network.py:79:21: E1120: No value for argument 'bridge1' in function call (no-value-for-parameter) Is there a flag for Pylint…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
3
votes
4 answers

How to create a function that takes dictionary inputs?

I am using a module for a project, I have to pass a function to the module and the model does something like: class module: def __init__(self, function, dictionary): # dictionary is {'x':2, 'y':4, 'z':23} function(**dictionary) And…
3
votes
1 answer

Replacing the parameters of a function with a vector

In Scala, if I have a function f(X, Y, Z) is it possible to pass a variable W that will represent (X, Y, Z) so I can use f(W) instead of f(X,Y,Z)? If so, how can this be done?
Jose Cabrera Zuniga
  • 2,348
  • 3
  • 31
  • 56
3
votes
1 answer

Why does set().union(*list1) gives me the union of two list inside a list?

I am doing an assignment, which requires me to do a code walkthrough. I would like to have a brief description on how set().union(*list1) works so i can answer during my walkthrough. list1 = [[1,2,3],[1,2,3,5,8]] x =…
3
votes
1 answer

How to unpack parameters in Python?

Is it possible to unpack parameters in python like in javascript? def foo([ arg ]): pass foo([ 42 ])
3
votes
2 answers

unpacking multiple arguments from a list in a for loop

I have two lists. d1 = ["'02/01/2018'", "'01/01/2018'", "'12/01/2017'"] d2 = ["'02/28/2018'", "'01/31/2018'", "'12/31/2017'"] I am trying to get these values to be unpacked in a for loop. for i,y in d1,d2: i,y = Startdate, Enddate I realize…
Rob Friedman
  • 95
  • 2
  • 8
3
votes
1 answer

What data model methods implement argument keyword unpacking?

I have a class that I would like to be able to unpack into an argument list using the *args and **kwargs syntax. class MyFoo: x = 1 y = 2 # unpack x and y z = 3 # do not unpack z def bar(x, y): print(x, y) def baz(a, b=2, x=100,…
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
1 2
3
9 10