How to return more than one variable from a function in Python?
5559
I have a function where I need to do something to a string.  I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string.    In C#, I would use an out parameter for the string, but there is no equivalent in Python.  I'm still very new to Python and the only thing I can think of is to return a tuple with the boolean and modified string.  Related question:
21847
Given a function:  function x(arg) { return 30; }   You can call it two ways:  result = x(4); result = new x(4);   The first returns 30, the second returns an object.  How can you detect which way the function was called inside the function itself?  Whatever your solution is, it must work with the following invocation as well:  var Z = new x();  Z.lolol = x;  Z.lolol();   All the solutions currently think the Z.lolol() is calling it as a constructor.
53521
What JavaScript keywords (function names, variables, etc) are reserved?
2050
Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?
31933
I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?
15717
I'd like to do something like this:  &gt;&gt; foo = @() functionCall1() functionCall2()   So that when I said:  &gt;&gt; foo()   It would execute functionCall1() and then execute functionCall2().  (I feel that I need something like the )  EDIT:  functionCall1 and functionCall2 are not necessarily functions that return values.
2267
What's the difference between the list methods append() and extend()?
42444
How can I call an external program with a python script and retrieve the output and return code?
699
I am trying to return an array Data Member from one smaller 2D Array Object, and trying to insert the array into a larger 2D array object. But when attempting this, I came into two problems.  First problem is that I want to return the name of the 2D array, but I do not know how to properly syntax to return 2D Array name.  This is what my 2D Array data member looks like   private: int pieceArray[4][4]; // 2D Smaller Array   and I want to return this array into a function, but this one causes a compiler error:   int Piece::returnPiece() {     return pieceArray; //not vaild     // return the 2D array name }   I tired using this return type and it worked:   int Piece::returnPiece() {     return pieceArray[4][4]; }   But I am unsure if this is what I want, as I want to return the array and all of it's content.  The other problem is the InsertArray() function, where I would put the returnPiece() function in the InsertArray()'s argument.  The problem with the InsertArray() is the argument, heres the code for it:   void Grid::InsertArray( int arr[4][4] ) //Compiler accepts, but does not work {     for(int i = 0; i &lt x_ROWS ; ++i)     {          for (int j = 0; j &lt y_COLUMNS ; ++j)          {              squares[i][j] = arr[i][j];          }     } }   The problem with this is that it does not accept my returnPiece(), and if i remove the "[4][4]", my compiler does not accept.  Mostly all these are syntax errors, but how do I solve these problems?   Returning the whole pieceArray in returnPiece() The correct syntax for the argument in InsertArray() The argument of InsertArray() accepting the returnPiece()   These 3 are the major problems that I need help with, and had the same problem when I attempt to use the pointer pointer method. Does anyone know how to solve these 3 problems?
27911
Say I have a C function which takes a variable number of arguments: How can I call another function which expects a variable number of arguments from inside of it, passing all the arguments that got into the first function?  Example:  void format_string(char *fmt, ...);  void debug_print(int dbg_lvl, char *fmt, ...) {     format_string(fmt, /* how do I pass all the arguments from '...'? */);     fprintf(stdout, fmt);  }
72069
Given [1,2,3,4,5], how can I do something like   1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5   I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I would like to return (1,5).  So basically I would like to do something like  for each element i in the list map some function across all elements in the list, taking i and j as parameters store the result in a master list, find the minimum value in the master list, and return the arguments i, jused to calculate this minimum value.  In my real problem I have a list objects/coordinates, and the function I am using takes two coordinates and calculates the euclidean distance. I'm trying to find minimum euclidean distance between any two points but I don't need a fancy algorithm.
123777
What is the difference between the three functions and when to use them??
4130
Python's  function returns the sum of numbers in an iterable.  sum([3,4,5]) == 3 + 4 + 5 == 12   I'm looking for the function that returns the product instead.  somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60   I'm pretty sure such a function exists, but I can't find it.
78279
I'm sure there's a simpler way of doing this that's just not occurring to me.  I'm calling a bunch of methods that return a list.  The list may be empty.  If the list is non-empty, I want to return the first item; otherwise, I want to return None.  This code works:  my_list = get_list() if len(my_list) &gt; 0: return my_list[0] return None   It seems to me that there should be a simple one-line idiom for doing this, but for the life of me I can't think of it.  Is there?  Edit:  The reason that I'm looking for a one-line expression here is not that I like incredibly terse code, but because I'm having to write a lot of code like this:  x = get_first_list() if x:     # do something with x[0]     # inevitably forget the [0] part, and have a bug to fix y = get_second_list() if y:     # do something with y[0]     # inevitably forget the [0] part AGAIN, and have another bug to fix   What I'd like to be doing can certainly be accomplished with a function (and probably will be):  def first_item(list_or_none):     if list_or_none: return list_or_none[0]  x = first_item(get_first_list()) if x:     # do something with x y = first_item(get_second_list()) if y:     # do something with y   I posted the question because I'm frequently surprised by what simple expressions in Python can do, and I thought that writing a function was a silly thing to do if there was a simple expression could do the trick.  But seeing these answers, it seems like a function is the simple solution.
4021
What is the best way to go about calling a function given a string with the function's name in a Python program.  For example, let's say that I have a module foo, and I have a string whose content is "bar". What is the best way to call foo.bar()?  I need to get the return value of the function, which is why I don't just use eval. I figured out how to do it by using eval to define a temp function that returns the result of that function call, but I'm hoping that there is a more elegant way to do this.
79724
I'm looking for something similar to this, or other:  len(bpy.context.selected_objects)
82094
How can I build a loop in JavaScript?