Is there a Python shortcut for variable checking and assignment?

2024/10/3 2:27:53

I'm finding myself typing the following a lot (developing for Django, if that's relevant):

if testVariable then:myVariable = testVariable
else:# something else

Alternatively, and more commonly (i.e. building up a parameters list)

if 'query' in request.POST.keys() then:myVariable = request.POST['query']
else:# something else, probably looking at other keys

Is there a shortcut I just don't know about that simplifies this? Something with the kind of logic myVariable = assign_if_exists(testVariable)?

Answer

Assuming you want to leave myVariable untouched to its previous value in the "not exist" case,

myVariable = testVariable or myVariable

deals with the first case, and

myVariable = request.POST.get('query', myVariable)

deals with the second one. Neither has much to do with "exist", though (which is hardly a Python concept;-): the first one is about true or false, the second one about presence or absence of a key in a collection.

https://en.xdnf.cn/q/70775.html

Related Q&A

python scipy Delaunay plotting point cloud

I have a pointlist=[p1,p2,p3...] where p1 = [x1,y1],p2=[x2,y2] ...I want to use scipy.spatial.Delaunay to do trianglation on these point clouds and then plot itHow can i do this ?The documentation for…

Pythonic way to verify parameter is a sequence but not string

I have a function that gets a list of DB tables as parameter, and returns a command string to be executed on these tables, e.g.:pg_dump( file=/tmp/dump.sql,tables=(stack, overflow),port=5434name=europe…

How to get a random (bootstrap) sample from pandas multiindex

Im trying to create a bootstrapped sample from a multiindex dataframe in Pandas. Below is some code to generate the kind of data I need.from itertools import product import pandas as pd import numpy a…

Python Regex - replace a string not located between two specific words

Given a string, I need to replace a substring with another in an area not located between two given words.For example:substring: "ate" replace to "drank", 1st word - "wolf"…

Vectorized Lookups of Pandas Series to a Dictionary

Problem Statement:A pandas dataframe column series, same_group needs to be created from booleans according to the values of two existing columns, row and col. The row needs to show True if both cells …

Why cant I get my static dir to work with django 1.3?

This problem is very simple, but I just cant figure it outadded to my urlpatternsurl(r^static/(?P<path>.*)$, django.views.static.serve, {document_root: /home/user/www/site/static})where my main.…

Desktop Launcher for Python Script Starts Program in Wrong Path

I can not launch a python script from a .desktop launcher created on Linux Mint 17.1 Cinnamon.The problem is that the script will be launched in the wrong path - namely the home folder instead of the d…

How does numpy broadcasting perform faster?

In the following question, https://stackoverflow.com/a/40056135/5714445Numpys broadcasting provides a solution thats almost 6x faster than using np.setdiff1d() paired with np.view(). How does it manage…

python check if utf-8 string is uppercase

I am having trouble with .isupper() when I have a utf-8 encoded string. I have a lot of text files I am converting to xml. While the text is very variable the format is static. words in all caps should…

Failed building wheel for Twisted in Windows 10 python 3

Im trying to install rasa-core on my windows 10 machine.When installing with pip install, I get: Failed building wheel for TwistedThe same error appears when trying to install Twisted separately.How co…