About 122,000 results
Open links in new tab
  1. return variables from functions python - Stack Overflow

    Jul 20, 2017 · For example, you seem to think that the variables pword and uname inside your functions are related to the variables with the same names in the global scope, but those variables are local to your function (they have to be, since you made them parameters to your functions!). Even if you return them, that doesn't mean they are related.

  2. python - How do I get ("return") a result (output) from a function?

    The natural, simple, direct, explicit way to get information back from a function is to return it. Broadly speaking, the purpose of a function is to compute a value, and return signifies "this is the value we computed; we are done here". Directly using return. The main trick here is that return returns a value, not a variable.

  3. How can I return two values from a function in Python?

    Values aren't returned "in variables"; that's not how Python works. A function returns values (objects). A variable is just a name for a value in a given context. When you call a function and assign the return value somewhere, what you're doing is giving the received value a …

  4. python - How do I pass variables across functions? - Stack Overflow

    When you assign a variable in a function, you only assign it in the namespace of this function. But clearly you want to use it between all functions. def defineAList(): #list = ['1','2','3'] this creates a new list, named list in the current namespace.

  5. python - Is it pythonic for a function to return multiple values ...

    In other cases, you want to return multiple, closely linked pieces of data---as in the example given. In this setting, returning multiple values is akin to returning a single instance of an anonymous class with several member variables. Python's handling of method arguments necessitates the ability to directly return multiple values.

  6. function - Ignore python multiple return value - Stack Overflow

    Jan 10, 2009 · # The function you are calling. def func(): return 1, 2 # You seem to only be interested in the first output. x, temp = func() I have found the following to works, x, *_ = func() This approach "unpacks" with * all other variables into a "throwaway" variable _. This has the benefit of assigning the one variable you want and ignoring all ...

  7. Alternatives for returning multiple values from a Python function

    "Best" is a partially subjective decision. Use tuples for small return sets in the general case where an immutable is acceptable. A tuple is always preferable to a list when mutability is not a requirement. For more complex return values, or for the case where formality is valuable (i.e. high value code) a named tuple is better.

  8. python - How to use local variable in a function and return it?

    May 11, 2012 · If you need to set variables to values determined by a function, then you have the function return those values and you use them to set the variables. If the values the function is calculating depend on the values of variables you have in the calling scope, then you need to pass them to the function as arguments.

  9. How to specify multiple return types using type-hints

    Jul 3, 2024 · Python 3.10 or newer: Use |. Example for a function which takes a single argument that is either an int or str and returns either an int or str: def func(arg: int | str) -> int | str: # ^^^^^ ^^^^^ # type of arg return type Python 3.5 - 3.9: Use typing.Union:

  10. Python Return multiple Variables from a function

    Nov 18, 2014 · Not so much of a question as a valuable observation for people using python. Unlike the majority of other programming languages, you can return multiple variable from a function without dealing with objects, lists etc. simply put . return ReturnValue1, ReturnValue2, ReturnValue3 to return however many you wish.

Refresh