Опубликовано 8 августа, 200620 г. comment_2644985 Как отличить объект встроенного типа от сложного объекта(инстанцированного класса, например), от объекта-функции? Изменено 8 августа, 200620 г. пользователем Гость Жалоба
Опубликовано 14 августа, 200620 г. comment_2666845 Смотри модуль inspect - #3.11.1 в доке от python 2.4.3 Там, например, описана функция isbuiltin и подобные... А зачем это, если не секрет? ;-) Жалоба
Опубликовано 15 августа, 200620 г. Автор comment_2668141 QUOTE isbuiltin( object) Return true if the object is a built-in function. А мне нужно работать с типами. Хочу визуализировать объект в виде дерева подобъектов. def printObject(object, indent=0): retvalue="" indenter=" " indentString="" for i in range(0,indent): indentString=indentString+indenter if str(type(object)).find('instance') <0 : retvalue+=indentString + "'"+str(object)+"'" return retvalue if ( "__name__" in dir(object) and object.__name__ ) : retvalue+=indentString+"Name: " + str(object.__name__)+"\n" if ( "__class__" in dir(object) and object.__class__ ) : retvalue+=indentString+"Class: " + str(object.__class__)+"\n" if ( "__doc__" in dir(object) and object.__doc__ ) : retvalue+=indentString+"Desc: " + str(object.__doc__)+"\n" if (indent > 2): return "Depth limit" indentString=indentString+indenter for subObjectName in dir(object): if (subObjectName.startswith("__")): continue subObject=getattr(object, subObjectName) if str(type(subObject)).find('instance') >=0 : retvalue+=indentString + subObjectName+":"+"\n" retvalue+=printObject(getattr(object, subObjectName), indent+1)+"\n" else: retvalue+=indentString + subObjectName+"="+"'"+str(subObject)+"'"+"\n" return retvalue Признак объекта пользовательского класса: str(type(subObject)).find('instance') >=0 https://academ.club/html/emoticons/blink.gif - у скриптовых языков своя специфика, но такая проверка мне претит. Есть другие способы? Или может быть сразу функция визуализации? Изменено 15 августа, 200620 г. пользователем Гость Жалоба
Опубликовано 23 августа, 200620 г. comment_2702087 Вместо for i in range(0,indent): indentString=indentString+indenter лучше indentString = indenter * indent Вместо name in dir(obj) лучше использовать hasattr(obj, name), Ну и может сначала изучить модули repr (3.28) и pprint (3.27) и понять чем они тебе не милы? ;-) Изменено 23 августа, 200620 г. пользователем Гость Жалоба
Изменено пользователем Гость