Is it possible to have a function with two returns like this:
function test($testvar)
{
// Do something
return $var1;
return $var2;
}
If so, how would I be able to get each return separately?
Is it possible to have a function with two returns like this:
function test($testvar)
{
// Do something
return $var1;
return $var2;
}
If so, how would I be able to get each return separately?
Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):
$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>
$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>
$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>
The -d php_suffix=<version>
piece allows you to set config values at run time vs pre-setting them with pecl config-set
. The uninstall -r
bit does not actually uninstall it (from the docs):
vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages. More than one package may be
specified at once. Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)
Options:
...
-r, --register-only
do not remove files, only register the packages as not installed
...
The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).
Return a tuple:
func getTime() -> (Int, Int, Int) {
...
return ( hour, minute, second)
}
Then it's invoked as:
let (hour, minute, second) = getTime()
or:
let time = getTime()
println("hour: (time.0)")
Since the return statement in getName
specifies multiple elements:
def getName(self):
return self.first_name, self.last_name
Python will return a container object that basically contains them.
In this case, returning a comma separated set of elements creates a tuple. Multiple values can only be returned inside containers.
Let's use a simpler function that returns multiple values:
def foo(a, b):
return a, b
You can look at the byte code generated by using dis.dis
, a disassembler for Python bytecode. For comma separated values w/o any brackets, it looks like this:
>>> import dis
>>> def foo(a, b):
... return a,b
>>> dis.dis(foo)
2 0 LOAD_FAST 0 (a)
3 LOAD_FAST 1 (b)
6 BUILD_TUPLE 2
9 RETURN_VALUE
As you can see the values are first loaded on the internal stack with LOAD_FAST
and then a BUILD_TUPLE
(grabbing the previous 2
elements placed on the stack) is generated. Python knows to create a tuple due to the commas being present.
You could alternatively specify another return type, for example a list, by using []
. For this case, a BUILD_LIST
is going to be issued following the same semantics as it's tuple equivalent:
>>> def foo_list(a, b):
... return [a, b]
>>> dis.dis(foo_list)
2 0 LOAD_FAST 0 (a)
3 LOAD_FAST 1 (b)
6 BUILD_LIST 2
9 RETURN_VALUE
The type of object returned really depends on the presence of brackets (for tuples ()
can be omitted if there's at least one comma). []
creates lists and {}
sets. Dictionaries need key:val
pairs.
To summarize, one actual object is returned. If that object is of a container type, it can contain multiple values giving the impression of multiple results returned. The usual method then is to unpack them directly:
>>> first_name, last_name = f.getName()
>>> print (first_name, last_name)
As an aside to all this, your Java ways are leaking into Python :-)
Don't use getters when writing classes in Python, use properties
. Properties are the idiomatic way to manage attributes, for more on these, see a nice answer here.
Never used any of those, but they look interesting..
Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..
There is no way of returning 2 variables. Although, you can propagate an array and return it; create a conditional to return a dynamic variable, etc.
For instance, this function would return
$var2
In application:
If you wanted them both, you could modify the function a bit