conditional-operator

How does the Python conditional operator workaround work?

荒凉一梦 提交于 2019-11-27 20:33:19
From what I have read, I found that a built-in ternary operator does not exist (I will be happy to know more about it.). I found the following code as a substitute: def val(): var = float(raw_input("Age:")) status = ("Working","Retired")[var>65] print "You should be:",status I couldn't understand how this code works; can anyone explain me how actually the code is working? I am also interested to know why the ternary operator doesn't exist; any references or links about this will be ore useful. I'm running Python 2.6.4 on Windows Vista. Python has a construct that is sort of like the ternary

why do we prefer ? to ?? operator in c#?

百般思念 提交于 2019-11-27 20:11:36
问题 I recently found that we can use ?? operator to check nulls. Please check the below code samples: var res = data ?? new data(); This is exactly similar to var res = (data==null) ? new data() : data ; I checked my whole project source repository and some of other open source projects. And this ?? operator never been used. I just wondering is there any reason behind this, like performance problems or something? EDIT: I just updated my sample code based on the comments from recursive & Anton.

C# ?: Conditional Operator

江枫思渺然 提交于 2019-11-27 19:59:09
I have this extract of C# 2.0 source code: object valueFromDatabase; decimal result; valueFromDatabase = DBNull.Value; result = (decimal)(valueFromDatabase != DBNull.Value ? valueFromDatabase : 0); result = (valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0); The first result evaluation throws an InvalidCastException whereas the second one does not. What is the difference between these two? Eric Lippert UPDATE: This question was the subject of my blog on May 27th 2010 . Thanks for the great question! There are a great many very confusing answers here. Let me try to

twig: IF with multiple conditions

♀尐吖头ヾ 提交于 2019-11-27 19:57:00
问题 It seem I have problem with a twig if statement. {%if fields | length > 0 || trans_fields | length > 0 -%} The error is: Unexpected token "punctuation" of value "|" ("name" expected) in I can't understand why this doesn't work, it's like if twig was lost with all the pipes. I've tried this : {% set count1 = fields | length %} {% set count2 = trans_fields | length %} {%if count1 > 0 || count2 > 0 -%} but the if also fail. Then tried this: {% set count1 = fields | length > 0 %} {% set count2 =

Why doesn't the conditional operator correctly allow the use of “null” for assignment to nullable types? [duplicate]

我怕爱的太早我们不能终老 提交于 2019-11-27 16:06:41
Possible Duplicates: Nullable types and the ternary operator. Why won’t this work? Conditional operator assignment with nullable<value> types? This will not compile, stating "Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DateTime' and ''" task.ActualEndDate = TextBoxActualEndDate.Text != "" ? DateTime.Parse(TextBoxActualEndDate.Text) : null; This works just fine if (TextBoxActualEndDate.Text != "") task.ActualEndDate = DateTime.Parse(TextBoxActualEndDate.Text); else task.ActualEndDate = null; This doesn't work because the compiler

Is it possible to use EL conditional operator in action attribute?

懵懂的女人 提交于 2019-11-27 15:41:31
问题 The conditional operator works in many attributes like "rendered" "value" and others. But it does not work in action? Or am I doing it wrong? <h:commandLink action="#{true ? bean.methodTrue() : bean.methodFalse()}"/> Error: javax.el.ELException: Not a Valid Method Expression (I realized it using primefaces ajax action attribute) 回答1: This is not supported. The action attribute is supposed to be a MethodExpression , but the conditional operator makes it a ValueExpression syntax. I don't think

Using return in ternary operator

梦想与她 提交于 2019-11-27 15:29:51
I'm trying to use return in a ternary operator, but receive an error: Parse error: syntax error, unexpected T_RETURN Here's the code: $e = $this->return_errors(); (!$e) ? '' : return array('false', $e); Is this possible? Thanks! This is the correct syntax: return !$e ? '' : array('false', $e); Sysyphus Close. You'd want return condition?a:b It doesn't work in most languages because return is a statement (like if , while , etc.), rather than an operator that can be nested in an expression. Following the same logic you wouldn't try to nest an if statement within an expression: // invalid because

Are multiple conditional operators in this situation a good idea?

﹥>﹥吖頭↗ 提交于 2019-11-27 14:40:42
问题 I just saw this block of code on the Wikipedia article on conditional operators: Vehicle new_vehicle = arg == 'B' ? bus : arg == 'A' ? airplane : arg == 'T' ? train : arg == 'C' ? car : arg == 'H' ? horse : feet; I've changed the code a little, but the idea is the same. Would you find this use of the conditional operator acceptable? It's much more concise than the if - else construct, and using a switch would definitely open up a whole new set of opportunities for bugs (fall-throughs anyone?)

Why would you use the ternary operator without assigning a value for the “true” condition (x = x ?: 1)

◇◆丶佛笑我妖孽 提交于 2019-11-27 14:16:27
In the Android open-source qemu code I ran across this line of code: machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ Is this just a confusing way of saying: if (machine->max_cpus) { ; //do nothing } else { machine->max_cpus = 1; } If so, wouldn't it be clearer as: if (machine->max_cpus == 0) machine->max_cpus = 1; Interestingly, this compiles and works fine with gcc, but doesn't compile on http://www.comeaucomputing.com/tryitout/ . This is permitted in GNU as an obscure extension to C 5.7 Conditionals with Omitted Operands The middle operand in a conditional expression may be

Ternary operator in JSTL/EL

删除回忆录丶 提交于 2019-11-27 13:52:10
The following tag of JSTL can be used to set a value to a variable in a request scope. <c:set var="value" scope="request" value="someValue"/> I want to check conditionally, if the variable value being set is empty or not and display the result accordingly something like the following, using <c:when>...</c:when> . <c:choose> <c:when test="${not empty value}"> <c:out default="None" value="${value}"/> </c:when> <c:otherwise> <c:out default="None" value="None"/> </c:otherwise> </c:choose> I want to reduce the line of code using a ternary expression like, <c:out default="None" value="${not empty