2

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.

1
  • thanks @Eimantas, this makes my head hurt less now :) Commented Apr 10, 2011 at 6:07

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

short attention span. i tried to edit, then forgot the name of the function :^\
perfect! I added return testSomething() in the switch, and works as expected. :-)
0

Perhaps a sub-function is not the right encapsulation tool for you here. You want to expose internal functionality to an external entity. Python classes provide a better mechanism for expressing this than sub-function. Having a class, you can expose whatever parts of internal functionality you want in a very controlled manner.

1 Comment

thank you! I will give that a whirl a little down the road when I'm not on such a time constraint.
0

My first thought upon looking at your code is that it's a little too complicated. Why have aFunction at all? You could just write

def aModuleEntryPoint():
    argument = ...
    if argument in (theRightValue, theOtherRightValue, theOtherOtherRightValue)\
       and testSomething():
        doMoreStuff()
    else:
        complain()  

This if clause will first check whether argument is one of the possible right values, and if it is, then it will proceed to call testSomething() and check the return value of that. Only if that return value is true will it call doMoreStuff(). If either of the tests fails (that's why I used and), it will complain().

2 Comments

like I mentioned in the post, because of data encapsulation, aFunction() knows how to do things that others dont. In real life, aFunction() is called control(). It's purpose is to control the connection to sql. testSomething() is called checkConnection(), and verifies a good link to the database before the user of the program goes through the hassle of inputting data, only to be told the link failed.
@brad: ok, well it wasn't really clear from your question that aFunction contained enough code to justify making it its own function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.