Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x_Python 2.7 - Fatal编程技术网

如何在python中打印类的声明静态变量

如何在python中打印类的声明静态变量,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,下面是获取实例变量列表及其值的简单代码 下面的代码提供列表实例变量及其值的输出。我需要帮助来查找静态变量列表及其值 class Test: x = 10 def __init__(self): self.a =10 t = Test() print("Instance Variables:",t.__dict__) 实际结果: Instance Variables: {'a': 10} 预期成果: Instance Variables

下面是获取实例变量列表及其值的简单代码

下面的代码提供列表实例变量及其值的输出。我需要帮助来查找静态变量列表及其值

 class Test:
    x = 10
    def __init__(self):
        self.a =10

 t = Test()       
 print("Instance Variables:",t.__dict__)
实际结果:

 Instance Variables: {'a': 10}
预期成果:

 Instance Variables: {'a': 10}
 Static Variable: {'x':10}

类自动具有许多属性,但它们往往以
\uuuu
开头。因此,要获得其余部分,您可以使用以下方法:

{k:v for k,v in Test.__dict__.items() if not k.startswith('__') }
结果:

{'x': 10}

“静态”一词在Python中没有在java或C++中有相同的含义。Python的类名称空间只是另一个字典名称空间,就像实例可以有名称空间一样。函数和整数只是两种类型的Python对象,在类名称空间中可以引用任意数量的对象。