and-or trick in Python [duplicate] - python-2.7

This question already has answers here:
Is short-circuiting in assignment statements considered good style?
(2 answers)
Closed 9 years ago.
What's the purpose of the "and-or" trick?
i.e.
>>> a = ""
>>> b = "second"
>>> 1 and a or b
'second'

It was just a way to mimic the conditional operator (aka. "ternary" operator) found in the C family of languages. In the past, there was no direct equivalent expression. The following expressions are somewhat equivalent:
# in python
a and b or c
// in C
a ? b : c
Don't use it though. Due to the semantics of Python, if b was falsy, the expression will evaluate to c.
They have since provided a proper syntax for this construct as of Python 2.5 (PEP 308).
b if a else c

Related

What is this Called in C++? [duplicate]

This question already has answers here:
What is a designated initializer in C?
(2 answers)
Closed last month.
I've seen a piece of code that goes like this:
int charP[] = {
['A'] = 1,
['B'] = 2,
['C'] = 3,
['D'] = 4};
My syntax highlighter for C++ does not recognize it, but the syntax highlighter for C does. It sort of behaves like a Python dictionary, so charP['A'] would spit out 1. Maybe I'm not looking hard enough, but can anyone point me in the right direction because every time I search "C++ dictionary" or "C dictionary", it always involves Maps and not this. What is it? What are its limitations?
In C it's array initialization using a designated initializer. It's covered in section 6.7.9 of the C 2017 specification.
In C++ it's invalid syntax (#Ranoiaetep, #CaptainObvlious).

Is there a C or C++ equivalent to 'pass' in python? [duplicate]

This question already has answers here:
Is there an equivalent of Python's `pass` in c++ std11?
(7 answers)
Closed 2 years ago.
Is there a C or C++ equivalent to 'pass' in python? Also the same with break. For example:
while True:
if x == 1:
break
else:
pass
But in C/C++ instead?
Is there a C or C++ equivalent to 'pass' in python?
Yes. There are actually two equivalents. One is the null statement:
;
Another is the empty block statement:
{}
These are in most cases inter-changeable except the null statement cannot be used in all cases where the empty block statement can. For example, the function body must be a block statement.
In the example case, you can omit the else-statement entirely just like you can in Python.

Is there always the same execution sequence for various conditions in a If Statement in C++ for all compilers? [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
Lets assume a simple If Statement with two conditions A and B:
If ( condA && condB)
Is the Sequenz for all compilers the Same?
condition A
condition B
And is the execution of condition B therefore optional, in case condition A is already false?
Yes. Not evaluating condition B if A is false is called short circuit logic, and this behavior is guaranteed by the language specification.

In C++. whats the difference between (*a).b and a->b? [duplicate]

This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 3 years ago.
I am learning C++ in a advanced programming class from my work since I have only worked in Web and .NET languages so far.
In a midway test the instructor has marked all of my uses of (*a).b as wrong and deducted points for it, which could negatively affect my final score and I need a near perfect score to transision in work from web stack to application stack, so could some of you help me resolve this dispute?
If a is a pointer, there's no difference in functionality at all, and in fact one is expressed in terms of the other [expr.ref§2]:
The expression E1->E2 is converted to the equivalent form (*(E1)).E2; the remainder of [expr.ref] will address only the first option (dot).
If a is an instance of a class with overloaded operators * and ->, there could be a difference. But such a discrepancy would be surprising, and I'd consider the class to have a bug because of this.
In the end, it's all about convention and readability then. The -> operator exists as a shorthand for the */. pair, as it is shorter and has better precedence rules (no need for parentheses). Thus, one would indeed use it rather than */..

What is <?= in C++? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C extension: <? and >? operators
Take a look at the top answer (by sclo) to problem D of this Google Code Jam. It's C++ code, it must have compiled, and it contains statements such as this one:
double& ret = F[mask][cur][b];
if(j==cur) {
ret<?=f(tmp,j,b||bad[i])+M[cur][i]; // WTF is <?= ???
}
This doesn't compile in my Visual Studio 2008. What does the <?= mean?
It's a gcc extension: C extension: <? and >? operators
Recent manuals say:
The G++ minimum and maximum operators (‘<?’ and ‘>?’) and their compound forms (‘<?=’) and ‘>?=’) have been deprecated and are now removed from G++. Code using these operators should be modified to use std::min and std::max instead...
It's simply not valid C++. < Might be less than, an open angle bracket for a template argument list, or the start of a digraph however non of those can be followed by ?, then =.
It's a now deprecated g++ extension to the c++ language.
a <? b
is the minimum, returning the smaller of the numeric values a and b;
a >? b
is the maximum, returning the larger of the numeric values a and b.
There are also compound versions
<?=
and
>?=
that do assignment as well.
It doesn't compile also with GCC, and I've never heard of an operator <?=.
Anyway I would hazard a guess that a<?=b could have a semantics like: a = (a<b) ? b : a, but again, this is just a guess.