0

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

KC Ray
  • 77
  • 9

2 Answers2

1

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.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Thanks for the tips! As to "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."...the function's main job is actually processing multiple values at once. I didn't write the initial structure I am just trying to access the items in the list by inputting a column from another df (a column of numbers). If you have a way that you think would be better to approach the original function I am open to it! – KC Ray Nov 02 '21 at 23:59
  • @KCRay *If* it's justified, then by all means do it that way. I just thought it was worth cautioning against trying to shove too much functionality into the function, when that functionality can be achieved using existing constructs like `map` and comprehensions. – Carcigenicate Nov 03 '21 at 00:10
  • cool thanks! I haven't worked with ```map``` before so I appreciate you taking the time to present it as an option – KC Ray Nov 03 '21 at 00:14
1

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
intedgar
  • 631
  • 1
  • 11
  • Would you please mind expanding what you mean by this? I am not familiar with the *args parameter – KC Ray Nov 02 '21 at 23:51
  • 1
    Here more on this: https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – intedgar Nov 02 '21 at 23:58