我已经阅读了有关python中类似C的import { DeviceEventEmitter } from 'react-native';
componentWillMount() {
DeviceEventEmitter.addListener('SomeEventName', function(e: Event) {
// handle event.
});
}
componentWillUnmount () {
DeviceEventEmitter.removeListener('SomeEventName', this.onModalVisible)
}
习语的描述,该习语被表示为字典,大多数示例使用单个函数和break语句。就我而言,我有以下switch
if
我改写为:
if method == 'c3':
Deh = buildD4(I)
Dhe = buildD343(I-1)
elif method == 'c4':
Deh = buildD4(I);
Dhe = buildD4(I-1)
elif method == 'c5':
Deh = buildD343(I)
Dhe = buildD343(I-1)
elif method == 'e2':
Deh = buildAE4(I)
Dhe = buildAH4(I)
elif method == 'y2':
Deh = buildD2(I)
Dhe = buildD2(I-1)
dt=dx
我的问题是:
eh = {
'c3': buildD4(I),
'c4': buildD4(I),
'c5': buildD343(I),
'e2': buildAE4(I),
'y2': buildD2(I),
}
Deh = eh[method]
he = {
'c3': buildD343(I-1),
'c4': buildD4(I-1),
'c5': buildD343(I-1),
'e2': buildAH4(I),
'y2': buildD2(I-1),
}
Dhe = he[method]
if method == y2:
dt=dx
或Deh
时才对它们进行计算?1.1如果没有。我该如何实现?
Dhe
是最好的结构? PS:所有这些if/elif
函数均返回具有给定参数大小的方阵。 build*
是整数,I
和dt
是浮点数,dx
是字符串。
答案 0 :(得分:1)
否,必须首先构造字典,这意味着必须评估所有键和值。不过,您可以将函数存储为值:
data = {'a': lambda x: print(f"hello, {x}!"), 'b': lambda x: print(f"Hi, {x}!")}
data['a']("world")
1.1您可以执行以下操作:unevaluated = lambda: do_stuff()
。现在,do_stuff
仅在您调用unevaluated()
时才被调用,但这看起来很难看。
使用具有单个功能的词典可能是可行的方法