Home > Software engineering >  Dictionary-like python function won't accept more than one value as input
Dictionary-like python function won't accept more than one value as input

Time:11-03

Say I have a function that when you give it a number it returns a color and shape:

def metadata(number):
    """Define the state FIPS code and state name from a given state abbreviation.
    :param   number num -> number of object
    :return: color: str -> color of object
    :return: shape: str -> shape of object
     """

    if_number == 1:    color = 'red'; shape = 'square'
    if number == 2:    color = 'blue';  shape = 'triangle'
    if number == 3:    color = 'yellow';  shape = 'circle'

return color, shape 

How do I get the function to take in more than one argument? So I could run metadata(1,2,3) and it would output all the colors and shapes? I tried researching solutions but it is not quite a python dictionary so I haven't been able to find something similar. If you know a name for this kind of structure that would be helpful in my search! Not sure if there is a name for this if key = value: key = value; key = value function

CodePudding user response:

This is another way to look at the problem, but I wouldn't do like you're trying to do. I would simply map (or use a list comprehension) to apply your existing function to each piece of data:

for result in map(metadata, [1, 2, 3]):
    print(result)

('red', 'square')
('blue', 'triangle')
('yellow', 'circle')

As the other answer notes, you can do what you're trying to do with var-args:

def metadata(*numbers):
    results = []
    for number in numbers:
        if number == 1:    color = 'red'; shape = 'square'
        if number == 2:    color = 'blue';  shape = 'triangle'
        if number == 3:    color = 'yellow';  shape = 'circle'
    
        # Or make this a generator function that "yield"s results
        results.append(color, shape)
        
    return results

metadata(1, 2, 3)

But I would avoid doing this. Why does it matter to this function that you may want to process multiple items? It shouldn't care about that if its main job is to process a single item.


Also, your code will have buggy behavior if someone enters a value other than 1, 2, or 3. You should change those ifs to elifs, and have a catch-all else case to handle bad data.

CodePudding user response:

You can use the *args parameter

Edit: You put in *args as placeholder for multiple arguments in the function definitions. Then you could loop over the numbers.

def metadata(*numbers):
    """Define the state FIPS code and state name from a given state abbreviation.
    :param   number num -> number of object
    :return: color: str -> color of object
    :return: shape: str -> shape of object
     """
    results = []
    color = ""
    shape = ""
    for number in numbers:
        if number == 1:    
            color = 'red'
            shape = 'square'
        elif number == 2:    
            color = 'blue'  
            shape = 'triangle'
        elif number == 3:    
            color = 'yellow'  
            shape = 'circle'
        results.append([color, shape])

    return results
  • Related