Jeffrey Greenham
Jeffrey Greenham

Reputation: 1432

__name__ attribute in Python for Win32_ComputerSystem?

I'm trying to get the name of a WMI win32 class. But the __name__ attribute is not defined for it.

>> import wmi
>> machine = wmi.WMI()
>> machine.Win32_ComputerSystem.__name__

I get the following error:

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    machine.Win32_ComputerSystem.__name__
  File "C:\Python27\lib\site-packages\wmi.py", line 796, in __getattr__
    return _wmi_object.__getattr__ (self, attribute)
  File "C:\Python27\lib\site-packages\wmi.py", line 561, in __getattr__
    return getattr (self.ole_object, attribute)
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 457, in __getattr__
    raise AttributeError(attr)
AttributeError: __name__

I thought that the __name__ attribute is defined for all Python functions, so I don't know what the problem is here. How is it possible that this function doesn't have that attribute?

OK, The reason that I thought it was a method is because machine.Win32_ComputerSystem() is defined, but I guess that isn't enough for something to be a method. I realise that it isn't a method.

However, this doesn't work:

>> machine.Win32_ComputerSystem.__class__.__name__
'_wmi_class'

I want it to return 'Win32_ComputerSystem'. How can I do this?

Upvotes: 1

Views: 406

Answers (2)

Jeffrey Greenham
Jeffrey Greenham

Reputation: 1432

I've found a way to get the output that I want, however it doesn't satisfy me.

repr(machine.Win32_ComputerSystem).split(':')[-1][:-1]

returns: 'Win32_ComputerSystem'

There must be a more Pythonic way to do this.

Upvotes: 1

eldarerathis
eldarerathis

Reputation: 36203

From what I can tell looking at the documentation (specifically, based on this snippet), wmi.Win32_ComputerSystem is a class, not a method. If you want to get its name you could try:

machine.Win32_ComputerSystem.__class__.__name__

Upvotes: 2

Related Questions