【发布时间】:2026-01-19 11:20:03
【问题描述】:
我该怎么做? 可以我这样做吗?
def aFunction(argument):
def testSomething():
if thisValue == 'whatItShouldBe':
return True
else:
return False
if argument == 'theRightValue': # this is actually a switch using elif's in my code
testSomething()
else:
return False
def aModuleEntryPoint():
if aFunction(theRightValue) == True:
doMoreStuff()
else:
complain()
aModuleEntryPoint()
aModuleEntryPoint() 在开始做事之前需要首先确保一个条件为真。由于封装,aModuleEntryPoint 不知道如何检查条件,但aFunction() 有一个名为testSomething() 的子函数,它知道如何检查条件。 aModuleEntryPoint() 致电 aFunction(theRightValue)。
因为theRightValue 作为参数传递给aFunction(),所以aFunction() 调用testSomething()。 testSomething() 执行逻辑测试,并返回 True 或 False。
我需要aModuleEntryPoint() 知道testSomething() 决定了什么。我不想让aModuleEntryPoint() 知道testSomething() 是如何得出结论的。
在删除其他功能和其他功能的同时发布我的实际来源实际上是一项成就,因此我必须像这样设置一般要点。
【问题讨论】:
-
感谢@Eimantas,这让我的头现在不那么痛了:)
标签: python function return-value