如何用不同的名称初始化python类?

时间:2018-01-19 11:55:33

标签: python wmi

昨天我不得不将WMI用于python项目。我找到了python库here。 这个库真的有很好的文档,并且有很多工作样本。 e.g:

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 类初始化传递什么样的参数并查看源代码。我看起来像 类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

我看到有一个解释如何在评论中起作用,但无论如何我无法理解。

1 个答案:

答案 0 :(得分:1)

WMIWMI = connect的第1295行周围定义为wmi.py,不是类,而是connect()函数的另一个名称。 因此,要查看参数,请查看connect()