There's a bunch of things wrong with your code:
- Incorrect code invocation
- Incorrect function definition (placing a non-comment to the right of the colon)
- Poor function naming (far better to keep everything you use, so you don't have to debug every time you use it, but that requires descriptive naming)
- Failure to pass in the values to be used in the expression string
- No indentation (required part of the Python language)
- Failure to place a space between the
if
keyword and logical expression
- Using assignment (
=
) instead of equivalence (==
) for a logical test
- Using expression field delimiters in Python code
- Assuming a NULL value will match the string 'Null' (instead it maps to
None
)
- Improper use of early exit (after a
return
you don't need an else
)
- Performing string math (using
format
is best practice)
When the smoke clears, the corrected result looks like this:
Expression:
usefulName(!Street!,!StreetID!,'nullValue')
Expression type:
Python
Code block:
def usefulName(street_val,street_id,x):
if (street_id == None):
return x
return "{:s} {:s}".format(street_id,street_val.split("/")[0])
If you want to avoid early exit (some organizations/programs forbid it as confusing), then the code block would look like:
def usefulName(street_val,street_id,x):
result = x
if (street_id != None):
result = "{:s} {:s}".format(street_id,street_val.split("/")[0])
return result
And the ternary logic form would look like:
def usefulName(street_val,street_id,x):
return "{:s} {:s}".format(street_id,street_val.split("/")[0]) if street_id else x
Which actually compresses down to a simple Python expression of:
"{:s} {:s}".format(!StreetID!,!Street!.split("/")[0]) if !StreetID! else 'nullValue'
though that's probably a bit obscure for reuse.