-2
def my_function():
payload = {
            "key1": f"{value1}",
            "key2": "default value",
            "propertySet": [
                {
                    "key3": f"{value3}",
                    "key4": f"{value4}",
                    "key5": f"{value5}",
                    "content": [
                                    {
                                        "key6": "default value",
                                        "key7": "default value",                                  
                                        "key8": f"{value8}",

                                    },
                                    {
                                        "key9": "value9",
                                        "key10": "value10"
                                    }
                                ],
                    "key11": "value11"
                }
            ],
            "newRule": {
                "key12": f"{value12}"
            },
            "key13": "value13"
        }

what would be the better to pass values from value1 to value13 to my_function(). if i pass the vales as parameters in function then that would be too many parameters. appreciate ur help. thanks

  • 1
    what does the function take as input ? – Sanket Wagh Jul 08 '22 at 18:49
  • How are you collecting these values in the first place? Are they in a `dict` or `list` perhaps? – tdelaney Jul 08 '22 at 18:49
  • Six is hardly "too many parameters" in and of itself. Does it make sense for a single function to build the payload, or should you perhaps have separate functions, for example, to create the a property set and pass *that* to a function that assembles the payload? – chepner Jul 08 '22 at 18:52
  • input is in a list and it can be arrange in a dictionary as well – Ghazal Saeed Jul 08 '22 at 18:53
  • @chepner so if i create a separate function for property set then its also about 7 values to pass – Ghazal Saeed Jul 08 '22 at 18:56
  • You pass as many arguments as you need. How many you need depends on what you need to do. More important than how many arguments you pass is how much work a single function does. I'm talking about the difference between `foo(a, b, c, d, e, f, g)` and `foo(bar(a, b), baz(c, d), qux(e, f, g))`. (Lots of parameters is usually just an indication that your function is trying to do too many things, and switching `foo(a, b, c, d, e, f, g)` to `foo([a, b, c, d, e, f, g])` doesn't really address that.) – chepner Jul 08 '22 at 19:00

1 Answers1

0

Python f-strings execute expressions and that includes looking things up in lists. So, if you have these values in a well-ordered list, you can just pass that

def my_function(values: list): --> none
    payload = {
            "key1": f"{values[0]}",
            "key2": "default value",
etc...

Other options include using a dict (f"{values['value1']}") or a class such as types.SimpleNamespace (f"{values.value1}").

tdelaney
  • 73,364
  • 6
  • 83
  • 116