dir()
正如标题所说,飞镖上是否有相当于蟒蛇的东西?
1 回答
Python dir() 函数用于找出模块定义的名称。
我们可以使用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,镜像,_LocalObjectMirrorImpl,_LocalMirrorImpl,_makeSignatureString,_LocalTypeVariableMirrorImpl,评论,MirroredUncaughtExceptionError,_LocalParameterMirrorImpl,_LazyTypeVariableMirror,TypedefMirror,VariableMirror,IsolateMirror,currentMirrorSystem,_dartEscape,_LocalMethodMirrorImpl、ClosureMirror、VMReference、ParameterMirror、InstanceMirror、_isSimpleValue、SourceLocation]
这从字面上说明了特定库(模块)中定义的内容。现在,Python 的函数可能存在一些差异,它似乎也对名称进行了排序,但这应该让您有一个良好的开端。