如何在 Python 中的函数之间传递大量变量? [英] How do I pass lots of variables to and from a function in Python?

查看:63
本文介绍了如何在 Python 中的函数之间传递大量变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做科学编程,经常想向用户展示提示和变量对,让他们编辑变量,然后用新变量进行计算.我经常这样做,以至于我编写了一个 wxPython 类来将这段代码移出主程序.您为每个变量设置了一个列表,其中包含变量的类型(字符串、浮点数、整数)、提示和变量的当前值.然后,您将所有这些列表放在一个大列表中,我的实用程序会创建一个格式整齐的 wxPython 面板,其中包含提示和可以编辑的当前值.

I do scientific programming, and often want to show users prompts and variable pairs, let them edit the variables, and then do the calulations with the new variables. I do this so often, that I wrote a wxPython class to move this code out of the main program. You set up a list for each variable with the type of the variable (string, float, int), the prompt, and the variable's current value. You then place all of these lists in one big list, and my utility creates a neatly formated wxPython panel with prompts and the current values which can be edited.

刚开始的时候,我只有几个变量,所以我会写出每个变量.

When I started, I only had a few variables, so I would write out each variable.

s='this is a string'; i=1; f=3.14
my_list=[ ['s','your string here',s], ['i','your int here',i], ['f','your float here'],]
input_panel = Input(my_list)

 # the rest of the window is created, the input_panel is added to the window, the user is
 # allowed to make  choices, and control returns when the user hits the calculate button

s,i,f = input_panel.results()     # the .results() function returns the values in a list

现在我想对很多变量(10-30)使用这个例程,这种方法正在崩溃.我可以使用 list.append() 语句在多行中创建函数的输入列表.但是,当代码从函数返回时,我得到了这个巨大的列表,需要将其解压缩到正确的变量中.这很难管理,而且看起来很容易让输入列表和输出列表不同步.更糟糕的是,它看起来很笨拙.

Now I want to use this routine for a lot of variables (10-30), and this approach is breaking down. I can create the input list to the function over multiple lines using the list.append() statements. When the code returns from the function, though, I get this huge list that needs to be unpacked into the right variables. This is difficult to manage, and it looks like it will be easy to get the input list and output list out of sync. And worse than that, it looks kludgy.

将大量变量传递给带有额外信息的 Python 函数以便对其进行编辑,然后取回变量以便我可以在程序的其余部分中使用它们的最佳方法是什么?

What is the best way to pass lots of variables to a function in Python with extra information so that they can be edited, and then get the variables back so that I can use them in the rest of the program?

如果我可以通过引用将变量传递到函数中,那么用户可以更改或不更改它们,一旦程序从函数返回,我将使用这些值.我只需要在多行上构建输入列表,并且输入列表不会与输出列表不同步.但是 Python 不允许这样做.

If I could pass the variables by reference into the function, then users could change them or not, and I would use the values once the program returned from the function. I would only need to build the input list over multiple lines, and there wouldn't be any possiblity of the input list getting out of sync with the output list. But Python doesn't allow this.

我是否应该将大列表分解成较小的列表,然后将它们组合成大列表以传入和传出函数?或者这只是增加了更多出错的地方?

Should I break the big lists into smaller lists that then get combined into big lists for passing into and out of the functions? Or does this just add more places to make errors?

推荐答案

最简单的方法是创建一个类.类将具有属性,而不是处理变量列表.然后您只需使用该类的单个实例.

The simplest thing to do would be to create a class. Instead of dealing with a list of variables, the class will have attributes. Then you just use a single instance of the class.

这篇关于如何在 Python 中的函数之间传递大量变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆