Accessing only one of the return values from a function

marcb90

How do I only access one of the values:

my code:

def test():
    a = 4
    b = 5
    c = 6

    return a, b, c 

a = test().a  # only want "a" from the function
Tim Biegeleisen

You could return a dictionary instead:

def test():
    a = 4
    b = 5
    c = 6

    return {"a":a, "b":b, "c":c}

print(test()["a"])

4

If you want to stick with your current approach, then the best you might be able to do would be to just print the first element from the tuple returned:

print(test()[0])

But this of course means that the caller would have to know that a happens to coincide with the first value.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to return one and only one value from a PowerShell function?

分類Dev

Python: How to obtain return value from only one function (out of several executed with asyncio.gather)

分類Dev

How to get the return values from a function?

分類Dev

Function to return many values in one column based on multiple queries

分類Dev

Trying to return one dataset from a function that prints from two functions

分類Dev

How to return Multiple List<> values from a single function?

分類Dev

Accessing Properties of one Object from another Object

分類Dev

Use array and return values one by one

分類Dev

Return some values with PDO in function

分類Dev

Accessing Azure File Storage from Azure Function

分類Dev

Azure function accessing connection string from configuration

分類Dev

function works only with hardcoded values

分類Dev

Selecting unique rows from a table having duplicate values in only one column

分類Dev

How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

分類Dev

Find rows matching only one of two values

分類Dev

Make parameters that get from function to be able for accessing from others sub

分類Dev

How to compose functions that return Bools to one function

分類Dev

Return json data from Function

分類Dev

Return INSERT output from a function

分類Dev

How to return a list from a function?

分類Dev

Accessing nested values in JSON data from Twitch API

分類Dev

Can a function return multiple values of varying types?

分類Dev

Store function return values in igraph objects in R

分類Dev

Modifying a function to return an expression based on the values of a column

分類Dev

Trying to understand bash function return values

分類Dev

Coffeescript disregard some of multiple return values of a function

分類Dev

Accessing table returning by table-valued function from SP

分類Dev

Accessing Parent's Fragment function from Viewpager2 fragment

分類Dev

why select * from return only the first field?

Related 関連記事

  1. 1

    How to return one and only one value from a PowerShell function?

  2. 2

    Python: How to obtain return value from only one function (out of several executed with asyncio.gather)

  3. 3

    How to get the return values from a function?

  4. 4

    Function to return many values in one column based on multiple queries

  5. 5

    Trying to return one dataset from a function that prints from two functions

  6. 6

    How to return Multiple List<> values from a single function?

  7. 7

    Accessing Properties of one Object from another Object

  8. 8

    Use array and return values one by one

  9. 9

    Return some values with PDO in function

  10. 10

    Accessing Azure File Storage from Azure Function

  11. 11

    Azure function accessing connection string from configuration

  12. 12

    function works only with hardcoded values

  13. 13

    Selecting unique rows from a table having duplicate values in only one column

  14. 14

    How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

  15. 15

    Find rows matching only one of two values

  16. 16

    Make parameters that get from function to be able for accessing from others sub

  17. 17

    How to compose functions that return Bools to one function

  18. 18

    Return json data from Function

  19. 19

    Return INSERT output from a function

  20. 20

    How to return a list from a function?

  21. 21

    Accessing nested values in JSON data from Twitch API

  22. 22

    Can a function return multiple values of varying types?

  23. 23

    Store function return values in igraph objects in R

  24. 24

    Modifying a function to return an expression based on the values of a column

  25. 25

    Trying to understand bash function return values

  26. 26

    Coffeescript disregard some of multiple return values of a function

  27. 27

    Accessing table returning by table-valued function from SP

  28. 28

    Accessing Parent's Fragment function from Viewpager2 fragment

  29. 29

    why select * from return only the first field?

ホットタグ

アーカイブ