5 Ways To Improve Any Function in Python

学習

Here are five ways you can improve any function in Python.

▶ Become job-ready with Python:
https://www.indently.io

▶ Follow me on Instagram:
https://www.instagram.com/indentlyreels

00:00 Learning Python made simple
00:05 Intro
00:13 Getting started
01:19 Tip #1 – Meaningful names
01:44 Tip #2 – Typing
02:56 Tip #3 – Documentation
04:49 Tip #4 – Proper validation
06:38 Tip #5 – Prioritise readability
08:30 Comparison
10:00 Conclusion

コメント

  1. @DrDeuteron より:

    since the str object offers isdigit(), isalpha(), isalphanum() functions, which all follow a well known python builtin pattern, I would create my own version for vowels, which is:

    >>>isvowel = ‘aeiouAEIOUàáâäǎæãåāaèéêëěẽēėęìíîïǐĩīıįòóôöǒœøõōùúûüǔũūűű’.__contains__

    From there, summing booleans is a trick. A violation of POLA (principle of least astonishment), so I agree, it’s gotta go. We are NOT summing, we are COUNTING, so call a well known count method:

    >>>return list(map(isvowel, text)).count(True)

    That is tight, with zero “if” blocks and zero loops. Minimum cyclomatic complexity. No (implicit) casting bools to int, just counting True’s in a list. Clear as day.

    But, maybe too compact, so comment your code:

    try
    # create mapper to check is vowel
    is_vowel_map = map(isvowel, text)
    except TypeError as err:
    raise [some message] from err

    # check each character’s vowel status
    check_list = list(is_vowel_map)

    # count isvowel == True in check_list
    vowel_count = check_list.count(True)

    return vowel_count

  2. @AnasArfeen-r4j より:

    0:10 Fython idead sir

タイトルとURLをコピーしました