Skip Navigation
InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)BI
Posts
1
Comments
0
Joined
7 mo. ago
Python @programming.dev
bitscomplicated @lemmy.world

I am including the full text of the post


Despite not being a pure functional language, a lot of praise that python receives are for features that stem from functional paradigms. Many are second nature to python programmers, but over the years I have seen people miss out on some important features. I gathered a few, along with examples, to give a brief demonstration of the convenience they can bring.

Replace if/else with or

With values that might be None, you can use or instead of if/else to provide a default. I had used this for years with Javascript, without knowing it was also possible in Python.

 python
    
def get_greeting_prefix(user_title: str | None):
    if user_title:
        return user_title
    return ""

  

Above snippet can shortened to this:

 python
    
def get_greeting_prefix(user_title: str | None):
    return user_title or ""

  

Pattern Matching and Unpacking

The overdue arrival of match to python means that so many switch style statements are expresse