I've wrote a plugin and when I was tested it I found a plugin's case of use that it has to handle with null values. I've had some research and doesnt found much about it in PyQGIS3 documentation, but I read about NULL
from qgis.core
and write the code below:
from qgis.core import QgsProject, NULL
layer = QgsProject.instance().mapLayersByName('My_Layer_Name')[0]
selection = layer.selectedFeatures()
n_sel = layer.selectedFeatureCount()
if n_sel > 0:
layer.startEditing()
for feature in selection:
pos = feature['offset_quad']
if pos == NULL:
pos = 0
else:
pos = int(feature['offset_quad'])
# do some stuf here
I'd like to know if there is a better "pythonic" way to do this test for null values.
if pos is None
. I Think your code is ok, easy to understand. (You could shorten it withpos = 0 if pos is None else int(pos)
)print('null') if pos is None else print('not null')
and returnsnot null
. I'm missing something? I liked this way of shorten conditionals, where I can find some docs to learn more about it?pos
returns{QVariant}NULL
the statementif pos != None
evaluates to False butif pos is not None
returns True. Apparently a{QVariant}NULL
equalsNone
although it is notNone
. This creates an issue in variance with PEP8.