Create dictionary from/with function arguments as keys and default values as value

aaronlyy

I want to get a dictionary from a function that has the function arguments as keys and the default values as values.

I used inspect.getfullargspec() to get a list of the argument and a list of the default values. Then I tried to use dict(zip()) to create a dictionary of those lists. This works, but only if every arguments has a default value. If one has no default, everything gets mixed up (as expected).

How can I for example if a argument has no default, add a None as value. Or any other ideas?

Here's the code:

def fn(name, age=18):
    pass

spec = getfullargspec(fn)
args = spec.args
defaults = spec.defaults

zipped = dict(zip(args, defaults))
Carcigenicate

Since the non-default parameters must always be on the left (because non-defaulting parameters can't come after defaulting ones), you can calculate how much padding you need, then add that on to the front of the defaults:

def func(a, b=1, c=2, *args, **kwargs):
    pass

spec = getfullargspec(func)
padded_defaults = (None,) * (len(spec.args) - len(spec.defaults)) + spec.defaults

zipped = dict(zip(spec.args, padded_defaults))  # {'a': None, 'b': 1, 'c': 2}

(len(spec.args) - len(spec.defaults)) calculates how much left padding is required then generates that padding using "sequence multiplication" ((None,) *). + spec.defaults then concatenates the padding onto the left of the existing defaults.

This may break in some corner case; although I can't think of such a case off-hand. This also isn't very efficient due to the tuple concatenation, but that's likely not an issue in 99% of cases.

There does not seem to be a mapping between arg->default stored anywhere, so it seems like your best bet is inferring what arguments have what defaults based on the number of each.

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

From Pandas series, create dictionary with unique elements as keys, and their indices as values

分類Dev

Swap keys for values in dictionary?

分類Dev

Initialisation of object with init function that provides default values for it's arguments

分類Dev

Assign list values to dictionary keys

分類Dev

Assigning values to keys in a mutable dictionary

分類Dev

Match keys to values in dictionary in python

分類Dev

Compare keys of dictionary with values of another dictionary

分類Dev

Instead of read a dictionary by keys, read by values (mydict[key] = value >>> mydict[value] = key)

分類Dev

Construct a dictionary from a list of dictionary keys, sub-keys and values

分類Dev

How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

分類Dev

Default template arguments for function templates

分類Dev

Use a function with default and extra arguments

分類Dev

Generate a string from a dictionary keys (combinations) and assing a boolean value based on values

分類Dev

Filling a dataframe from a dictionary keys and values

分類Dev

retrieve values from dataframe using keys in dictionary

分類Dev

How to add multiple values to dictionary keys in Python

分類Dev

Edit nested dictionary duplicate values in same keys

分類Dev

How to sum up values of duplicate keys in a dictionary?

分類Dev

How to get dictionary keys and values if both keys are in two separated dictionaries?

分類Dev

Creating a dictionary with values identical to keys from another dictionary

分類Dev

Input as a default value for a function

分類Dev

KDB+/Q: How does one select all items (keys+values) of a dictionary where the respective value conforms to a condition?

分類Dev

How can I return common values of dictionary values for specific keys?

分類Dev

Python Dictionary using Lists as Values, find other Keys with same values

分類Dev

Wrap function without clobbering default arguments

分類Dev

Programmatically decide which arguments to pass to a function with default arguments?

分類Dev

default value for dictionary in jinja2 (ansible)

分類Dev

Method signature for a function with default values

分類Dev

Create json with column values as object keys

Related 関連記事

  1. 1

    From Pandas series, create dictionary with unique elements as keys, and their indices as values

  2. 2

    Swap keys for values in dictionary?

  3. 3

    Initialisation of object with init function that provides default values for it's arguments

  4. 4

    Assign list values to dictionary keys

  5. 5

    Assigning values to keys in a mutable dictionary

  6. 6

    Match keys to values in dictionary in python

  7. 7

    Compare keys of dictionary with values of another dictionary

  8. 8

    Instead of read a dictionary by keys, read by values (mydict[key] = value >>> mydict[value] = key)

  9. 9

    Construct a dictionary from a list of dictionary keys, sub-keys and values

  10. 10

    How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

  11. 11

    Default template arguments for function templates

  12. 12

    Use a function with default and extra arguments

  13. 13

    Generate a string from a dictionary keys (combinations) and assing a boolean value based on values

  14. 14

    Filling a dataframe from a dictionary keys and values

  15. 15

    retrieve values from dataframe using keys in dictionary

  16. 16

    How to add multiple values to dictionary keys in Python

  17. 17

    Edit nested dictionary duplicate values in same keys

  18. 18

    How to sum up values of duplicate keys in a dictionary?

  19. 19

    How to get dictionary keys and values if both keys are in two separated dictionaries?

  20. 20

    Creating a dictionary with values identical to keys from another dictionary

  21. 21

    Input as a default value for a function

  22. 22

    KDB+/Q: How does one select all items (keys+values) of a dictionary where the respective value conforms to a condition?

  23. 23

    How can I return common values of dictionary values for specific keys?

  24. 24

    Python Dictionary using Lists as Values, find other Keys with same values

  25. 25

    Wrap function without clobbering default arguments

  26. 26

    Programmatically decide which arguments to pass to a function with default arguments?

  27. 27

    default value for dictionary in jinja2 (ansible)

  28. 28

    Method signature for a function with default values

  29. 29

    Create json with column values as object keys

ホットタグ

アーカイブ