1

I'm trying to return a list in one of my methods for interacting with vmware via pyvmomi.

active_list = self.vss.spec.policy.nicTeaming.nicOrder.activeNic

when I do return active_list I get:

(str) [
   'vmnic0',
   'vmnic1'
]

Type:

type(active_list)
<class 'pyVmomi.VmomiSupport.str[]'>

Since I'm able to iterate over the active_list as it is, shoud I be concerned about the (str) prefix.

I was able to avoid the (str) [...] prefix by coping active_list, new_list=list(active_list)

What is the best pythonic approach to this?

jps
  • 20,041
  • 15
  • 75
  • 79
MMT
  • 1,931
  • 3
  • 19
  • 35

1 Answers1

2

This (str) prefix comes from the __str__() representation of the <class 'pyVmomi.VmomiSupport.str[]'>. Obviously the class also supports iterating, so you can loop over it.

You can learn more about special method names in the Docs.

As long as you don't rely on the str() output of that class / instance you can ignore it.

fechnert
  • 1,215
  • 2
  • 12
  • 30
  • what do you mean by `don't rely on the str()` it returns the items as strings – MMT Dec 21 '17 at 14:30
  • Not exactly, it gives you the items as string in a list as string, but it is not really a list. Is is in fact only a representation of this class. You can't use this representation as a list as-it. – fechnert Dec 21 '17 at 14:33
  • So I guess I'm safer with useing `new_active_list = list(active_list)` since I want to do comparison of lists `if set(active_list) == set(some_other_list)` – MMT Dec 21 '17 at 14:41
  • You could also compare your lists with the raw `active_list` instance, but this is also a good approach – fechnert Dec 21 '17 at 14:42