0

Struggling with grasping OOP and pyvmomi in general. Have a question about calling a specific folder by name - it seems that the object names can be folder or VM, and calling hasattr is the best way to differentiate the two?

Also, when I want to call the contents of a specific folder, I've been setting a variable with the name of the folder and looping through each object checking for a match, isn't there a better way, like calling specific object and folder name? I can't see to get it work. Here is what I have right now:

vmfolder = datacenter.vmFolder
vmfolderList = vmfolder.childEntity
folder = "FooFolder"
for i in vmfolderList:
    try:
        folderName = i.name
        if folderName == folder:
            vmfolder = i
            print(f"Working in folder: {folderName}")
    except:
        pass

What I have right now doesn't feel very pythonic. I used to write code years ago but it was entirely procedureal and the whole object/attribute thing is a struggle - if anyone recommends a good tutorial or resource as well, it would be appreciated. I feel that once I get over that hurdle I should be able to pick up any SDK and run with it.

ndizz
  • 11
  • 2

1 Answers1

0

Instead of testing using hasattr I would expect folder and VM objects to be of a different type. Checking for a type is entirely pythonic.

sophros
  • 14,672
  • 11
  • 46
  • 75