在python中测试从列表调用的函数
我想知道,如何测试从条件语句列表中随机抽取的函数?下面是一些示例代码。忽略代码应该打印的内容在python中测试从列表调用的函数,python,Python,我想知道,如何测试从条件语句列表中随机抽取的函数?下面是一些示例代码。忽略代码应该打印的内容 import random, time def biomeLand(): print "Biome: Land" def biomeOcean(): print "Biome: Ocean" def biomeDesert(): print "Biome: Desert" def biomeForest(): print "Biome: Forest" def b
import random, time
def biomeLand():
print "Biome: Land"
def biomeOcean():
print "Biome: Ocean"
def biomeDesert():
print "Biome: Desert"
def biomeForest():
print "Biome: Forest"
def biomeRiver():
print "Biome: River"
biomes = [biomeLand, biomeOcean, biomeDesert, biomeForest,
biomeRiver]
def run():
while True:
selected_biome = random.choice(biomes)()
time.sleep(0.5)
run()
再一次,当从列表中调用某个函数时,如何使程序在条件语句中进行测试?您可以像匹配任何其他变量一样匹配它们:
def foo():
print "foo"
def bar():
print "bar"
first = foo
print (first == bar) # prints "False"
print (first == foo) # prints "True"
因此,在您的示例中,您可以有如下内容:
if selected_biome == biomeLand:
# do something
也许:
def run():
while True:
selected_biome = random.choice(biomes)
selected_biome()
if selected_biome == biomeLand:
print "biomeLand Selected"
time.sleep(0.5)
run()