0

I've seen some code similar to:

nodeIds = Framework.getDataAsList(key) or []

What would be a full fragment of code it translates to? Is the function output being checked for False or for None outcome? Is there some similar Java syntax for this?

Vic
  • 21,473
  • 11
  • 76
  • 97

7 Answers7

6

or shortcircuits. It the expression on the left is "True" evaluates to that, otherwise it evaluates to the expression on the right (even if it is "False")

If the expression on the left is "True" the expression on the right will not even be evaluated. This is important if it has side-effects

"True" means that bool(expression) == True
"False" means that bool(expression) == False

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
2

there is pseudocode:

def nonZero(var):
   if isinstance(var,collection): 
     if len(collection) == 0 : return False
     else: return True
   if isinstance(var, number):
     if number == 0 : return False
     else: return True
   if isinstance(var, boolean): return var
   if isinstance(var, NoneType): return False
   if hasattr(var,'__nonzero__'): return var.__nonzero__()


nodeIds = Framework.getDataAsList(key)
if not nonZero(nodeIds) : nodeIds = []
Odomontois
  • 15,918
  • 2
  • 36
  • 71
  • Looks good! But as stated by @Hamish, the original syntax executes `Framework.getDataAsList()` twice, isn't it? – Vic Aug 15 '12 at 07:27
2

A longer version (in Python) might be:

nodeIds = Framework.getDataAsList(key)
if not nodeIds:
    nodeIds = []

Which might translate into Java almost identically:

Vector<foo> nodeIds = Framework.getDataAsList(key);
if(nodeIds == null) {
    nodeIds = new Vector<foo>();
}

(my Java is pretty rusty though ;)

Certain values in a 'not' expression evaluate to False. You can try this yourself from the interactive Python command line:

>>> not 0
... True
>>> not 1
... False
>>> not []
... True
>>> not ()
... True
>>> not True
... False
>>> not False
... True

Hope this helps.

lsh
  • 658
  • 5
  • 12
1

Assuming that Framework.getDataAsList(key) returns the same result for subsequent calls, it is equivalent to:

nodeIds = Framework.getDataAsList(key) if Framework.getDataAsList(key) else []

And:

if Framework.getDataAsList(key):
    nodeIds = Framework.getDataAsList(key)
else:
    nodeIds = []

Note, however, that both these examples require two calls to Framework.getDataAsList(key) instead of one. So, perhaps more accurately:

nodeIds = Framework.getDataAsList(key)
if not nodeIds:
    nodeIds = []
Hamish
  • 22,860
  • 8
  • 53
  • 67
1
>>> 0 or "yes"
'yes'
>>> None or "yes"
'yes'
>>> False or "yes"
'yes'
>>> [] or "yes"
'yes'
>>> "no" or "yes"
'no'

so if the function returns any value that is not __nonzero__() then an empty list is assigned. See also this part of the official tutorial on the short-circuit behavior of the boolean operators and and or. See also this answer for more on truth value testing of objects in Python.

Note: from Python 3.0 it's __bool__() that is being tested.


To Java, this could probably translated using the ternary operator:

result = Framework.getDataAsList(key);
nodeIds = result ? result : someEmptyList();

Provided the equivalent boolean conversion of your result object

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
0

It is checked for False. So if Framework.getDataAsList(key) returns [], {}, 0, "", (), None or any other value that evaluates to False, then [] will be assigned to nodeIDs.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

This Python idiom is common and used in assignments much like the ternary operator from other c-like languages.

# python
var = value or fallback

is equivalent to:

/* c-like */
var = value ? value : fallback

both are equivalent to the following pseudo-code:

if value is true:
    var = value
else:
    var = fallback
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153