Python return values?

22,322

The only thing I see wrong right now is you need a return before testSomething() on line 9.

Share:
22,322
brad
Author by

brad

Updated on April 08, 2020

Comments

  • brad
    brad about 4 years

    How do I do this? Can I do this?

    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() needs to first make sure that a condition is true before it starts doing things. Because of encapsulation, aModuleEntryPoint doesn't know how to check the condition, but aFunction() has a sub-function called testSomething() that does know how to check the condition. aModuleEntryPoint() calls aFunction(theRightValue).

    Because theRightValue was passed to aFunction() as an argument, aFunction() calls testSomething(). testSomething() performs the logic test, and either returns True or False.

    I need for aModuleEntryPoint() to know what testSomething() decided. I do not want aModuleEntryPoint() to know anything about how testSomething() came to its conclusion.

    It would actually be an accomplishment to post my actual source while removing other functions and what-not, so I had to setup the general gist like this.