列出Python中函数所需的变量?

列出Python中函数所需的变量?,python,function,variables,metaprogramming,Python,Function,Variables,Metaprogramming,我想知道在调用Python函数之前,是否可以列出Python函数所期望的变量,以便从包含大量变量的较大dict中传递期望的变量 我搜索了一下网,但什么也没找到。但是,python解释器可以显示预期变量的列表,因此一定有某种方法可以在脚本中执行此操作。您可以使用或函数: import inspect argspec = inspect.getfullargspec(somefunction) signature = inspect.signature(somefunction) inspect

我想知道在调用Python函数之前,是否可以列出Python函数所期望的变量,以便从包含大量变量的较大dict中传递期望的变量

我搜索了一下网,但什么也没找到。但是,python解释器可以显示预期变量的列表,因此一定有某种方法可以在脚本中执行此操作。

您可以使用或函数:

import inspect

argspec = inspect.getfullargspec(somefunction)
signature = inspect.signature(somefunction)
inspect.fullargspec
返回包含7个元素的命名元组:

  • 包含参数名称的列表
  • catchall
    *args
    参数的名称(如果已定义)(
    None
    否则)
  • catchall
    **kwargs
    参数的名称(如果已定义)(
    None
    否则)
  • 具有关键字参数默认值的元组;它们与论点的最后部分相一致;按默认值元组的长度匹配这些值
  • 仅关键字参数名称的列表
  • 仅关键字参数名称(如果有)的默认值字典
  • 和一本包含注释的词典
使用
inspect.signature()
您将获得一个丰富的对象,它不仅将上述数据建模为一组更结构化的对象,而且还允许您以调用函数的方式将值绑定到参数

哪一个更好将取决于您的用例

演示:


上面给出了仅位置参数的值列表,以及其余参数的字典(任何
*args
**kwargs
参数除外)。

作为一个附带答案,我现在使用另一种方法来传递函数期望的变量:我全部传递它们

我的意思是,我在我的根对象(它是所有其他对象的父对象)中维护一种全局/共享的变量术语,例如:

然后我简单地将这个dict传递给任何其他要调用的对象的任何方法,就像这样:

shareddict.update(call_to_func(**shareddict))
如您所见,我们将shareddict中的所有键/值解包为关键字参数,以调用_to_func()。我们还使用返回的结果更新shareddict,我们将在下面看到原因

现在有了这项技术,如果我需要本条中的一个或多个变量,我可以在我的函数/方法中简单而清晰地定义:

my_method1(A=None, *args, **kwargs):
''' This method only computes on A '''
    new_A = Do_some_stuff(A)
    return {'A': new_A} # Return the new A in a dictionary to update the shared value of A in the shareddict

my_method2(B=None, *args, **kwargs):
''' This method only computes on B '''
    new_B = Do_some_stuff(B)
    return {'B': new_B} # Return the new B in a dictionary to update the shareddict

my_method3(A=None, B=None, *args, **kwargs):
''' This method swaps A and B, and then create a new variable C '''
    return {'A': B, 'B': A, 'C': 'a_new_variable'} # Here we will update both A and B and create the new variable C
正如您所注意到的,上面的所有方法都返回一个变量dict,它将更新shareddict,并传递给其他函数

这种技术有几个优点:

  • 实现起来非常简单
  • 维护共享变量列表但不使用全局变量的优雅方式
  • 函数和方法在其定义中清楚地显示了它们所期望的内容(当然,有一个警告是,即使是强制变量也需要设置为具有默认值(如None)的关键字参数,这通常意味着变量是可选的,但在这里不是
  • 这些方法是可继承和可重载的
  • 内存占用率低,因为始终传递相同的SharedAct
  • 子函数/方法定义它们所需要的(自下而上),而不是定义将传递给子函数的参数的根(自上而下)
  • 非常容易创建/更新变量
  • 或者,很容易将所有这些变量转储到文件中,例如使用
    json.dumps(finaldict,sort\u keys=True)
又好又简单:

import inspect  #library to import  
def foo(bar, baz, spam='eggs', *monty, **python): pass  #example function

argspec = inspect.signature(foo)
print(argspec) #print your output
印刷品:(吧台,baz,spam='eggs',蒙蒂,**蟒蛇)

它也适用于类内的方法(非常有用!):


打印:(realpart,imagpart)

谢谢您的提醒。
inspect.signature()在Python 3.3中引入了,所以在这个问题的时候它是不可用的。但是现在,这可能是最好的解决方案,因为它也返回注释/预期类型的参数,如果是可用的:那么对于现代PythOnistas,请考虑这个答案而不是旧的接受的。
shareddict.update(call_to_func(**shareddict))
my_method1(A=None, *args, **kwargs):
''' This method only computes on A '''
    new_A = Do_some_stuff(A)
    return {'A': new_A} # Return the new A in a dictionary to update the shared value of A in the shareddict

my_method2(B=None, *args, **kwargs):
''' This method only computes on B '''
    new_B = Do_some_stuff(B)
    return {'B': new_B} # Return the new B in a dictionary to update the shareddict

my_method3(A=None, B=None, *args, **kwargs):
''' This method swaps A and B, and then create a new variable C '''
    return {'A': B, 'B': A, 'C': 'a_new_variable'} # Here we will update both A and B and create the new variable C
import inspect  #library to import  
def foo(bar, baz, spam='eggs', *monty, **python): pass  #example function

argspec = inspect.signature(foo)
print(argspec) #print your output
class Complex: #example Class
     def __init__(self, realpart, imagpart): #method inside Class
...         self.r = realpart
...         self.i = imagpart

argspec = inspect.signature(Complex)
print(argspec)