Assigning one of multiple values based on what exists
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 fig开发者_开发问答ured 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?
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.
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.
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
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;
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
.
精彩评论