python class 如何用不同的名称初始化?
How does python class initialized with different name?
昨天我不得不为 python 项目使用 WMI。我找到 python 库 here。
这个库确实有很好的文档记录,并且有很多工作示例。例如:
import wmi
c = wmi.WMI ()
for process in c.Win32_Process ():
print process.ProcessId, process.Name
或
import wmi
c = wmi.WMI ("some_other_machine")
我很好奇我可以为 WMI class 初始化传递什么样的参数,并查看了源代码。我看起来像 class WMI: 或函数 def WMI(),但是我发现是这样声明的:
#
# class WMI
#
class _wmi_namespace:
"""A WMI root of a computer system. The classes attribute holds a list
of the classes on offer. This means you can explore a bit with
things like this::
c = wmi.WMI ()
for i in c.classes:
if "user" in i.lower ():
print i
"""
def __init__ (self, namespace, find_classes):
_set (self, "_namespace", namespace)
#
# wmi attribute preserved for backwards compatibility
#
_set (self, "wmi", namespace)
self._classes = None
self._classes_map = {}
#
# Pick up the list of classes under this namespace
# so that they can be queried, and used as though
# properties of the namespace by means of the __getattr__
# hook below.
# If the namespace does not support SubclassesOf, carry on
# regardless
#
if find_classes:
_ = self.classes
如我所见,评论中有解释它是如何工作的,但无论如何我还是看不懂。
WMI
被定义为 wmi.py
中第 1295 行附近的 WMI = connect
并且不是 class,而是 connect()
函数的另一个名称。
因此,要查看参数,请查看 connect()
.
昨天我不得不为 python 项目使用 WMI。我找到 python 库 here。 这个库确实有很好的文档记录,并且有很多工作示例。例如:
import wmi
c = wmi.WMI ()
for process in c.Win32_Process ():
print process.ProcessId, process.Name
或
import wmi
c = wmi.WMI ("some_other_machine")
我很好奇我可以为 WMI class 初始化传递什么样的参数,并查看了源代码。我看起来像 class WMI: 或函数 def WMI(),但是我发现是这样声明的:
#
# class WMI
#
class _wmi_namespace:
"""A WMI root of a computer system. The classes attribute holds a list
of the classes on offer. This means you can explore a bit with
things like this::
c = wmi.WMI ()
for i in c.classes:
if "user" in i.lower ():
print i
"""
def __init__ (self, namespace, find_classes):
_set (self, "_namespace", namespace)
#
# wmi attribute preserved for backwards compatibility
#
_set (self, "wmi", namespace)
self._classes = None
self._classes_map = {}
#
# Pick up the list of classes under this namespace
# so that they can be queried, and used as though
# properties of the namespace by means of the __getattr__
# hook below.
# If the namespace does not support SubclassesOf, carry on
# regardless
#
if find_classes:
_ = self.classes
如我所见,评论中有解释它是如何工作的,但无论如何我还是看不懂。
WMI
被定义为 wmi.py
中第 1295 行附近的 WMI = connect
并且不是 class,而是 connect()
函数的另一个名称。
因此,要查看参数,请查看 connect()
.