0

I use if isinstance(ins,list): to check . but it returned false Although ins is the List[Object]

def getname(ins):
    name=[]
    if isinstance(ins,list):
        for i in ins:
            name.append(i.Name)
    else:
        name.append(ins.Name)
    return name

Levels = FilteredElementCollector(doc).OfClass(Level).ToElements()
ULevels = UnwrapElement(Levels)
Levelsname = getname(ULevels)

Error message is:

AttributeError: 'List[object]' object has no attribute 'Name'

yatu
  • 86,083
  • 12
  • 84
  • 139
HuyPham
  • 1
  • 1
  • 1
    The simple explanation is that `ULevels.Name` does not exist. Why did you expect it to? – John Gordon Sep 12 '19 at 14:08
  • Levels and ULevels are returning the list[object] (some case can be object) , these object can get name property. my problem here is isinstance( Ulevels, list) return "false" value. I expect it should be "true" to make Ulevels go to for..i loop. – HuyPham Sep 13 '19 at 03:17

1 Answers1

0

You can do it in a single line of code like this:

[UnwrapElement(x).Name for x in FilteredElementCollector(doc).OfClass(Level).ToElements()]

Since I can see you are using Dynamo you can also do it like this:

enter image description here

konrad
  • 3,544
  • 4
  • 36
  • 75
  • But do you know what happened with my problem? – HuyPham Sep 26 '19 at 02:31
  • sure. your check for `isinstance(ins, list)` checks the `ins` object if it is a Python list, and that returns `False` since it's a .NET type List. It then proceeds to call `ins.Name` and you get that error. So the issue you see is because `UnwrapElement()` method returns a .NET typed list. Try not to mix .NET objects with Python objects to avoid this. – konrad Sep 26 '19 at 16:50