Skip to content
Advertisement

I want to extract all the values that a class object holds

I have an object Vm of Type <class 'azure.mgmt.compute.v2019_07_01.models._models_py3.VirtualMachine'> I want to iterate over this object by a loop so that I don’t have to manually extract the values.

Yes I’ve tried isinstance to check whether the value is of type <class 'azure.mgmt.compute.v2019_07_01.models._models_py3.VirtualMachine'> and extract but in the next iteration it fails.

For example let’s consider vm to be an object

  • to access the vm size I will have to do vm.hardware_profile.vm_size
  • and similarly to access os type value I will have to do this vm.storage_profile.os_disk.os_type

What I’m trying to achieve here is that if I pass the vm object it should return all the end values. Any help or tips are greatly appreciated and thanks in advance

The code that I have written

JavaScript

And the output that i’ve been getting

JavaScript

Output that I expect:

it should extract all the objects which are there in the Input

Advertisement

Answer

You can use recursion. Here I’m using hasattr to check if it’s has __dict__ attribute(vars() method return the results of obj.__dict__).

JavaScript

Example:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement