Difference between revisions of "Python/ThreePartOperator"

From ProgrammingExamples
Jump to: navigation, search
m
m (minor correction)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Python does not supply the ?: operator, but it is sometimes very nice. Here's how to mimic it
+
Early versions of Python did not supply a "ternary operator", but it is sometimes very nice.  
  
We often want to set local variables to "what was passed, or if not, a default" like this:
+
If you simply want a default value in case the initial value evaluates to <code>False</code> you can use the <code>or</code> operator which takes the leftmost operand that evaluates to <code>True</code> or the right operand if neither evaluates <code>True</code>. Like this:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
def example(required, alist=None, anum=None, astring=''):
 
def example(required, alist=None, anum=None, astring=''):
 
   my_list = alist or ['item']
 
   my_list = alist or ['item']
   my_num = anum or 666
+
   my_num = anum or 0 # note that 0 evaluates False, but is still chosen here
 
   my_string = astring or 'string'
 
   my_string = astring or 'string'
 
   print('required: "%s"\nlist: %s\nNumber: %s\nString: "%s"'%(required, my_list,my_num,my_string))
 
   print('required: "%s"\nlist: %s\nNumber: %s\nString: "%s"'%(required, my_list,my_num,my_string))
 
example('must') # prints 'must' plus the three defaults
 
example('yes',anum=99) # prints 'yes', default, 99, default
 
# etc
 
 
</syntaxhighlight>
 
</syntaxhighlight>
Note the use of the '''or''' operator. Python uses lazy left to right evaluation of that operator, so if the left operand evaluates as True the assignment takes it, else it takes the right operand.
+
Since version 2.5, however, Python has an official  [http://docs.python.org/reference/expressions.html#conditional-expressions ternary operator] called a "conditional expression": <code>x if C else y</code> evaluates C first, then either x or y.
 
+
There are some issues (what if you want the default to be something that evaluates as False?) but this technique will work often enough to be worth knowing.
+

Latest revision as of 14:55, 5 October 2011

Early versions of Python did not supply a "ternary operator", but it is sometimes very nice.

If you simply want a default value in case the initial value evaluates to False you can use the or operator which takes the leftmost operand that evaluates to True or the right operand if neither evaluates True. Like this:

def example(required, alist=None, anum=None, astring=''):
  my_list = alist or ['item']
  my_num = anum or 0 # note that 0 evaluates False, but is still chosen here
  my_string = astring or 'string'
  print('required: "%s"\nlist: %s\nNumber: %s\nString: "%s"'%(required, my_list,my_num,my_string))

Since version 2.5, however, Python has an official ternary operator called a "conditional expression": x if C else y evaluates C first, then either x or y.