Dup Ver Goto 📝

5 Ways To Improve Any Function in Python

To
49 lines, 122 words, 978 chars Page '5_Ways_To_Improve_Functions' does not exist.

Source: this Indently video

def vowels(text):
    return sum(char.lower() in 'aeiou' for char in text)

# 1 meaningful name
def count_vowels(text):
    ...

# 2 add type annotations
def count_vowels(text: str) -> int:
    ...

# 3 documentation
def count_vowels(text: str) -> int:
    """
    Counts vowels in text

    :param text: ...
    :return: ...
    """

# 4 validation
def count_vowels(text: str) -> int:
    """
    ...
    """
    if not isistnace(text,str):
        raise TypeError(f"Please only use strings, {type(text)} is not a valid type")
    ...

# 5 Prioritise readability
def count_vowels(text: str) -> int:
    ...
    num_vowels = 0

    for char in text:
        if char.lower() in "aeiou":
            num_vowels += 1

    return num_vowels