chuky pedro
chuky pedro

Reputation: 745

Why is my check for when a function argument is not of a given type not working?

I have a very simple function, which takes as input a string, I tried to put a check on the function so that if the function arguments is not a string it will throw an error, else return the string.

The problem right now is that, when I pass an int, I expect the function to display an error but it simply outputs a tuple of int. What am I getting wrong here?

def speak(*languages):

    """Function takes a set of langs as input  and returns a list of languages"""
    
    try:
        if  type(languages) ==  str:
            languages = list(languages)
    except TypeError as error:
        print(error, ' You need to pass in the right datatype')
    else:
        return languages

I also tried this.

def speak(*languages):

    """Function takes a set of langs as input  and returns a list of languages"""
    
    try:
        if  isinstance(languages, str):
            languages = list(languages)
    except TypeError as error:
        print(error, ' You need to pass in the right datatype')
    else:
        return languages

I am calling the speak function the following ways.

  1. speak('english') - expected output should be english
  2. speak('english', 'french') - expected output should be english and french
  3. speak(34) - I expect the function to throw an error

Upvotes: 0

Views: 108

Answers (4)

DeusDev
DeusDev

Reputation: 548

I don't think a try-except is needed here. Maybe try something like this (following your reasoning):

def speak(*languages):

    """Function takes a set of langs as input  and returns a list of languages"""
    for lang in list(languages):
        if not isinstance(lang, str):
            print(' You need to pass in the right datatype')
            return
    else:
        languages = list(languages)
        return languages

print(speak('english')) # english
print(speak('english', 'french')) # english and french
print(speak(34)) # error

Upvotes: 0

Sven Eberth
Sven Eberth

Reputation: 3116

After you provided your example calls, it looks like you want to check all arguments for string-type and not one argument. Right?

You can use any for that, which will be True if any of the list/generator value is true. In the generator comprehension you can perform the isinstance over all (positional) arguments

def speak(*languages):
    """Function takes a set of langs as input  and returns a list of languages"""
    
    if any(not isinstance(lang, str) for lang in languages):
        raise TypeError('You need to pass in the right datatype')

    return list(languages)
>>> speak('english')
>>> speak('english', 'french')
>>> speak(34)
Traceback (most recent call last):
  File "<string>", line 14, in <module>
  File "<string>", line 5, in speak
TypeError: You need to pass in the right datatype

Upvotes: 3

Shar
Shar

Reputation: 507

You aren't actually throwing an error here, to do so, you need to use raise:

def speak(*languages):

    """Function takes a set of langs as input  and returns a list of languages"""
    
    if  isinstance(languages, str):
        languages = list(languages)
        
    else:
        raise TypeError('You need to pass in the right datatype')
        
    return languages

Upvotes: -1

0x5453
0x5453

Reputation: 13589

Neither type(languages) == str nor isinstance(languages, str) will throw a TypeError, so your except will never be hit. You simply need an else after your if:

def speak(*languages):
    """Function takes a set of langs as input and returns a list of languages"""

    if isinstance(languages, str):
        return list(languages)
    else:
        raise TypeError('You need to pass in the right datatype')

Note: I don't think you want list(languages) above - list("a str") gives ['a', ' ', 's', 't', 'r']. But I left it because it's not relevant to the question.

Upvotes: -1

Related Questions