marlon
marlon

Reputation: 7723

How to allow multiple types of arguments in function?

def extraction(content: str,  channels: List[Color]=[Color.Red]):
  pass

The second argument channels is a List of Color. However, what about if I also want to allow the possibility of channels being a str argument? In that case, it becomes:

def extraction(content: str,  channels: str=None):
      pass

Is it possible to combine the two definitions into one function definition?

Upvotes: 0

Views: 3661

Answers (2)

Max Berktold
Max Berktold

Reputation: 63

To better implement this do the following:

from typing import Union

def extraction(content: str,  channels: Union[str, List[Color]]) :
    if type (channels) is str :
        print ('channels is a string.')
    if type (channels) is list :
        print ('channels is a list.')

extraction ('testing', ['one, two three'])

Upvotes: 1

bashBedlam
bashBedlam

Reputation: 1500

def extraction(content: str,  channels) :
    if type (channels) is str :
        print ('channels is a string.')
    if type (channels) is list :
        print ('channels is a list.')

extraction ('testing', ['one, two three'])

Upvotes: 2

Related Questions