简体   繁体   中英

How to pass multiple arguments in a cleaner way in python

I have the following code that I wanted to know if there is a cleaner version to do the following. The function basically accepts some data dict and parses it, manipulates it, constructs another dictionary, and finally sends some initial data values along with constructed dict to another function to store in db.

Can anyone help me put this structure in a better way?

def process_metadata(data):

    # extract data fields 

    # here result constructs another metadata from some of the above values

    result = {
                            "identifier": _id,
                            "type": ["t1", "t2", "t3", t4],
                            "properties": {
                                "p1": v1,
                                "p2": v2,
                                "p3": v3,
                                "p4": v4,
                                "p5": v5,
                                "p6": v6,
                                "p7": v7,
                                "p8": v8,
                                "p9": "v9",
                                "p10": v10,
                                "p11": v11,
                                "p12": v12,
                                "p13": v13,
                                "p14": v14,
                                "p15": v15,
                                "p16": v16,
                                "p17": v17,
                                "p18": v18,
                                "p19": "v19",
                                "p20": v20,
                                "p21": v21,
                                "p22": "v22",
                                "p23": "v23",
                                "p24": v24,
                                "p25": v25,
                                "p26": v26,
                                "p27": v27,
                                "p28": v28,
                                "p29": v29,
                                "p30": v30,
                                "p31": v31,
                                "p32": v32,
                                "p33": v33,
                                "p34": v34,
                                "p35": v35,
                                "p36": "v36",
                                "p37": v37
                            }
                        }
    return another_function(data, result)

您可以将属性作为一个类,构造函数接受 v1-v37 并将实例化的属性对象存储在结果中,然后您的结果将具有三个字段,您可以通过result["properties"].v32访问您的属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM