Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Walrus Operator in Python 3.8
- # Look this video
- # https://www.youtube.com/watch?v=-Rh4XW89VlQ
- # The walrus operator is not used to get as many statements on one line
- # It's used to prevent calling functions twice or more.
- request = {'from': {'username': 'Andre', 'password': input('Please something: ')}}
- db = []
- def process_from(request):
- """
- Example function
- """
- password = request['from']['password']
- # here we ask for the len of password
- if len(password) > 5:
- db.append(password)
- return 'Added user!'
- else:
- # here we're doing it again
- return f'Password is too short! The provided Password is only {len(password)} chars long'
- def process_from_pre38(request):
- """
- Working example for Python <= 3.7
- """
- password = request['from']['password']
- # get the length of password one time
- length = len(password)
- if length > 5: # use length here
- db.append(password)
- return 'Added user!'
- else:
- # and here
- return f'Password is too short! The provided Password is only {length} chars long'
- def process_from_walrus(request):
- """
- Same walrus operator
- Requires Python 3.8
- """
- password = request['from']['password']
- if (length := len(password)) > 5:
- db.append(password)
- return 'Added user!'
- else:
- return f'Password is too short! The provided Password is only {length} chars long'
- print(process_from(request))
- print(process_from_pre38(request))
- print(process_from_walrus(request))
- print(db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement