tags:

views:

121

answers:

5

In Perl, if I want to assign $myVar a value of $var1, $var2, or $var3, based on which one evaluates to true, I would code the following:

$myVar = $var1 || $var2 || $var3;

I am working on separate projects in both Python and PHP, and I have not figured out how to code this situation as concisely in either language.

What is the best way to code this in Python?

What is the best way to code this in PHP?

+2  A: 

In Python, I believe you can say

myVar = var1 or var2 or var3

The Python documentation describes x or y as

if x is false, then y, else x

Edit: theatrus brings up an interesting point - I hadn't even considered that the variables might not be defined. I found this discussion on the matter:

Python doesn't have a specific function to test whether a variable is defined, since all variables are expected to have been defined before use - even if initially just assigned the None object. Attempting to access a variable that hasn't previously been defined will raise an exception.

It is generally considered unusual in Python not to know whether a variable has already been defined. But if you are nevertheless in this situation, you can make sure that a given variable is in fact defined (as None, if nothing else) by attempting to access it inside a 'try' block and assigning it the None object should it raise a NameError exception.

So, if you're not guaranteed that the variables exist, and you want to check one by one for both existence and truthiness, you could be in for some messy code.

danben
This only works when var1/2/3 are declared but possibly False. If var1/2/3 are not declared, it results in a name error.
Yann Ramin
That would cause an exception if `var1` didn't *exist*.
badp
You could check the `locals()` and `globals()` dictionaries to see if the variable is defined but all that is simply not the Python way of doing it.
Noufal Ibrahim
+1  A: 

In PHP you can use isset() to test to see if the variable is set and not null. To achieve what you are looking for you can check whether $var1, $var2 or $var3 is set, and then set the value to $myVar

But beware depending on the condition of which you want to set your $myVar to, this would evaluate to true:

$str = '';
var_dump(isset($str)); // TRUE
Anthony Forloney
+3  A: 

Perl 'autovivifies' variables upon first reference (as far as I remember). Python will raise a NameError if a variable doesn't exist. However, you can do something like this.

var1 = var2 = var3 = None
# code that might change the value of three variables mentioned above
myvar = var1 or var2 or var3

Generally speaking, checking if a variable is defined (and altering the control flow based on that) is a bad thing in Python. You should use a dictionary and keys.

Noufal Ibrahim
You've got the terminology wrong in regard to Perl. If you aren't using the `strict` pragma, variables need not be declared before use (just like PHP). It is considered a best-practice to always use strict, and turn it off as needed over the smallest scope possible. Autovivification is different. What it means is that if you have a deep data structure (eg a 4 dimensional array), you can simply say `$array[1][3][2][0] = 5;` without having to do `$array[1] = []; $array[1][3]=[]; $array[1][3][2] = []; $array[1][3][2][0] = 5;`. All the intermediate structures are autovivified.
daotoad
Thanks for the clarification! :)
Noufal Ibrahim
A: 

There are also conditional expressions in Python added in Python 2.5:

x = true_value if condition else false_value

I cannot speak to PHP.

One thing to keep in mind with both languages is that trying to evaluate a variable that does not exist is always going to throw an error. With Python, you could work around this by catching the NameError exception that will occur if you attempt this and fallback to a default:

>>> try: 
...     myVar = var1 or var2 or var3
... except NameError:
...     myVar = 'default'
... 
>>> myVar
'default'

So if var1, var2, or var3 are either not set, then myVar will receive the fallback value of "default". Otherwise, you'll get the value of whichever var of the set evaluated to True.

jathanism
+1  A: 

In PHP you could do this (assumes the variables are set):

$vars = array($var1, $var2, $var3);
foreach ($vars as $var) {
    if ($myvar = $var) {
        break;
    }
}

Or, to make it slightly more perly :) :

foreach(array($var1,$var2,$var3)as$var)if($myvar=$var)break;
GZipp