正如标题所说,在飞镖上是否有相当于python的dir()
?
答案 0 :(得分:9)
Python dir() function用于找出模块定义的名称。
我们可以使用Mirrors并自己编写一个等效函数(或者至少非常相似):
import 'dart:mirrors';
List<String> dir([String libraryName]) {
var lib, symbols = [];
if (?libraryName) {
lib = currentMirrorSystem().libraries[libraryName];
} else {
lib = currentMirrorSystem().isolate.rootLibrary;
}
lib.members.forEach((name, mirror) => symbols.add(name));
return symbols;
}
现在这是一个例子:
class Hello {}
bar() => print('yay');
main() {
var foo = 5;
print(dir()); // [main, bar, Hello, dir]
}
或指定一个库:
print(dir('dart:mirrors'));
[MirroredError,TypeMirror,ObjectMirror,_LazyLibraryMirror, TypeVariableMirror,MirrorException,ClassMirror,MirrorSystem, _LocalMirrorSystemImpl,_LocalVMObjectMirrorImpl,DeclarationMirror,_LazyTypeMirror,_LocalClosureMirrorImpl,mirrorSystemOf,_LazyFunctionTypeMirror,_filterMap,MirroredCompilationError,_Mirrors,_LocalClassMirrorImpl,_LocalInstanceMirrorImpl,_LocalTypedefMirrorImpl, _LocalFunctionTypeMirrorImpl,reflect,MethodMirror,_LocalVariableMirrorImpl,LibraryMirror,_LocalIsolateMirrorImpl,FunctionTypeMirror,_LocalLibraryMirrorImpl,Mirror, _LocalObjectMirrorImpl,_LocalMirrorImpl,_ makeSignatureString,_LocalTypeVariableMirrorImpl,Comment,MirroredUncaughtExceptionError,_LocalParameterMirrorImpl,_LazyTypeVariableMirror,TypedefMirror,VariableMirror,IsolateMirror,currentMirrorSystem,_dartEscape, _LocalMethodMirrorImpl,ClosureMirror,VMReference,ParameterMirror,InstanceMirror,_isSimpleValue,SourceLocation]
这从字面上说明了在特定库(模块)中定义的内容。现在,Python的功能可能存在一些差异,这似乎也可以对名称进行排序,但这应该会给你一个良好的开端。