我尝试通过以下步骤检索内置类型
首先,使用' set'
检索builtins
中的所有类型
total_builtins = dir(__builtins__)
total_types = {type(eval(i)) for i in total_builtins}
In [95]: total_types
Out[95]:
{NoneType,
NotImplementedType,
_sitebuiltins._Helper,
_sitebuiltins._Printer,
bool,
builtin_function_or_method,
ellipsis,
function,
method,
str,
type}
#Subtract type of 'type'
total_types = total_types.pop()
In [120]: len(total_types)
Out[120]: 10
第二组他们使用dict,
In [117]: {mytype:[i for i in total_bins if isinstance(eval(i), mytype)]
for mytype in total_types}
Out[117]:
{NoneType: ['None', '__loader__', '__package__', '__spec__'],
NotImplementedType: ['NotImplemented'],
_sitebuiltins._Helper: ['help'],
_sitebuiltins._Printer: ['copyright', 'credits', 'license'],
bool: ['False', 'True', '__IPYTHON__', '__debug__'],
builtin_function_or_method: ['__build_class__',
'__import__',
....],
ellipsis: ['Ellipsis'],
function: ['display'],
method: ['get_ipython'],
str: ['__doc__', '__name__']}
这确实产生了预期的结果,但对于任务来说看起来有点过分(尤其是多重嵌套的理解)。
如何以聪明的方式解决这个问题?
答案 0 :(得分:2)
IIUC,你可以迭代= link_to '#', aria_label: "Change Icon", class: 'btn btn-info', id: 'icon-button' do
%i{class: icon, id: 'icon-button', data: {icon: icon}}
%span.sr-only
Change Icon
。虽然,你仍然需要在之后反转字典。
private void turnOnScreen() {
PowerManager.WakeLock screenLock = null;
if ((getSystemService(POWER_SERVICE)) != null) {
screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire(10*60*1000L /*10 minutes*/);
screenLock.release();
}
}
使用__builtins__.__dict__
-
d = {k : type(v) for k, v in builtins.__dict__.items()}
d2 = {}
for k, v in d.items():
d2.setdefault(v, []).append(k)
这比第一个解决方案更有效。