Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中处理并将大量变量传递给函数\对象_Python_Argument Passing - Fatal编程技术网

在python中处理并将大量变量传递给函数\对象

在python中处理并将大量变量传递给函数\对象,python,argument-passing,Python,Argument Passing,我发现自己需要处理包含大量变量的函数和对象 对于特定的情况,考虑来自一个分离的模块的函数,该函数取n个不同的变量,然后将它们传递给新的实例对象: def Function(Variables): Do something with some of the variables object1 = someobject(some of the variables) object2 = anotherobject(some of the variables, not nece

我发现自己需要处理包含大量变量的函数和对象

对于特定的情况,考虑来自一个分离的模块的函数,该函数取n个不同的变量,然后将它们传递给新的实例对象:

def Function(Variables): 
    Do something with some of the variables
    object1 = someobject(some of the variables)
    object2 = anotherobject(some of the variables, not necessarily as in object1)
虽然我可以传递一长串的变量,但有时我会发现自己对一个函数进行更改,这需要对它可能调用的其他函数或它可能创建的对象进行更改。有时变量列表可能会有一些变化

有没有一种优雅的方法可以传递大量变量并保持灵活性

我尝试以下列方式使用kwargs:

def Function(**kwargs):

    Rest of the function
调用函数(**somedict),其中somedict是一个字典,它有我需要传递给函数的所有变量的键和值(可能还有更多)。但是我得到一个关于未定义的全局变量的错误

编辑1:

由于我现在不在家或实验室,我将稍后发布这段代码。在那之前,我会尽力更好地解释情况

我有一个分子动力学模拟,需要几十个参数。很少有参数(例如温度)需要迭代。为了充分利用四核处理器,我并行运行了不同的迭代。因此,代码从不同迭代上的循环开始,并在每次传递时将该迭代的参数发送到工作人员池(使用多处理模块)。它是这样的:

P = mp.pool(number of workers) # If i remember correctly this line 
for iteration in Iterations:
     assign values to parameters
     P.apply_async(run,(list of parameters),callback = some post processing)
P.close()
P.join()
函数run获取参数列表并生成模拟对象,每个对象都将一些参数作为其属性

编辑2:

下面是有问题函数的一个版本<代码>**kwargs包含“sim”、“lattice”和“adatom”所需的所有参数

def run(**kwargs): 
"""'run' runs a single simulation process.
j is the index number of the simulation run.
The code generates an independent random seed for the initial conditios."""
scipy.random.seed()
sim = MDF.Simulation(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat,savetemp)
lattice = MDF.Lattice(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, kb, ks, kbs, a, p, q, massL, randinit, initvel, parangle,scaletemp,savetemp,freeze)
adatom = MDF.Adatom(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, ra, massa, amorse, bmorse, r0, z0, name, lattice, samplerate,savetemp,adatomrelax)                

bad = 1 
print 'Starting simulation run number %g\nrun' % (j+1)    
while bad is 1:
    # If the simulation did not complete successfuly, run it again.
    bad = sim.timeloop(lattice,adatom1,j)
print 'Starting post processing'
# Return the temperature and adatomś trajectory and velocity
List = [j,lattice.temp , adatom1.traj ,adatom1.velocity, lattice.Toptemp,     lattice.Bottomtemp, lattice.middletemp, lattice.latticetop]
return  List        

最干净的解决方案是根本不在函数中使用那么多参数

您可以使用set方法或属性分别设置每个变量,将它们存储为类成员并由该类中的函数使用

这些函数填充私有变量,可以使用get方法检索这些变量


另一种方法是使用结构(或不带函数的类),通过这种方式创建一组命名的变量。

将*args和/或**kwargs作为函数定义的参数列表中的最后一项,使该函数可以接受任意数量的匿名和/或关键字参数


当您不确定可以向函数传递多少个参数时,可以使用*args。

您还可以将参数分组到元组中以形成结构

这不像使用结构或get/set方法那样优雅,但可以在现有应用程序中轻松应用,无需太多返工

当然,元组中只应分组相关参数

例如,您可以将一个函数作为

值=功能调用((汽车型号,汽车类型),年龄,(owner.name,owner.address,owner.telephone))


这并没有减少参数的数量,而是增加了一点结构。

您的思路是正确的,可以使用*args或**kwargs,因此代码中可能有错误。请发布它,以便我们可以看到发生了什么。长参数列表是一种“代码气味”或反模式,并且有文档化的解决方案来解决它们(通常涉及结构或类)。