I see that it is possible to define a variable inside a region, but then refer to it outside this region. For example, the following code works:
if condition: x = 5 else: x = 10 print x
However, this seems a little strange to me. If you try to do this in C, the variable X will not be correctly defined:
if(condition) { int x = 5; } else { int x = 10; } print x;
The solution, in any case, is to first declare X, THEN to figure out what to do with it:
int x; if(condition) { x = 5; } else { x = 10; } print x;
So in Python, my instinct is to write code as follows:
x = None if condition: x = 5 else: x = 10 print x
However, I understand that Python does not require this from me. Any suggestions? Is there any style for this scenario?
Stephen gross
source share