简体   繁体   中英

keyword error generated when Passing a dictionary to a function with tuples as the keys

I am new to Python and I am struggling with the task of passing a dictionary, whose keys are tuples, as an argument to a function.

mydict = {('hostabc', 'pola'): 333444567, ('hostdef', 'polb'): 111222333, ('hostghi', 'polc'): 222999888}

def tupletest(**kwargs):
    print(kwargs)

tupletest(**mydict)

The following keyword error is generated:

TypeError                                 Traceback (most recent call last)
<ipython-input-29-fec409a1eb53> in <module>
      2 def tupletest(**kwargs):
      3     print(kwargs)
----> 4 tupletest(**mydict)

TypeError: tupletest() keywords must be strings

I am unsure if this is even possible given the error msg. I am testing this in 3.7.4

All help appreciated.

I performed a little example. It ist possible:

mydict = {('hostabc', 'pola'): 333444567, ('hostdef', 'polb'): 111222333, ('hostghi', 'polc'): 222999888}

def tupletest(kwargs):
    for key in kwargs:
        #print("key: %s , value: %s" % (key, kwargs[key]))
        print(key[0])
        print(key[1])

tupletest(mydict)

I hope this helps you. I also implemented a little example for entering the key of the dictionary.

Output

Short answer is, no it's not possible.

complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})

**kwargs represents keyword arguments. These arguments are unpacked (so to say) and passed to the function. That way you can use them in the function without having to explicitly pass them to the function as the positional-or-keyword arguments.

def func(*args, **kwargs): ...

var-keyword: specifies that arbitrarily many keyword arguments can be provided (in addition to any keyword arguments already accepted by other parameters). Such a parameter can be defined by prepending the parameter name with **, for example kwargs in the example above.

https://docs.python.org/3/glossary.html#term-argument

@BoarGules has nicely pointed you to the path. I have nothing new to add and I am saying below the same thing but in a little verbose manner.

See this nice discussion here . So dictionary keys become the named parameters to the function. In this particular case however the keys are tuples. The keyword must have an associated string property and that is what the error is saying above. Notice the " strings " in the error message below.

TypeError: tupletest() keywords must be strings

Had your dictionary been simpler like below, it would have worked.

mydict = {"a": 333444567, "b": 111222333, "c": 222999888}


def tupletest(**kwargs):
    for k in kwargs:
        print(k)

tupletest(**mydict)

The above gives this.

a
b
c

If you would rather want to have the tuples, I will take the dangerous route of eval after quoting the tuples.

mydict = {"('hostabc', 'pola')": 333444567, "('hostdef', 'polb')": 111222333, "('hostghi', 'polc')": 222999888}

def tupletest(**kwargs):
    for k in kwargs:
        print(eval(k))

tupletest(**mydict)

This gives the following output.

('hostabc', 'pola')
('hostdef', 'polb')
('hostghi', 'polc')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM