0

I have a program that defines the function verboseprint to either print or not print to the screen based on a boolean:

# define verboseprint based on whether we're running in verbose mode or not
if in_verbose_mode:
    def verboseprint (*args):
        for arg in args:
            print arg,
        print
    print "Done defining verbose print."
else:
    # if we're not in verbosemode, do nothing
    verboseprint = lambda *a: None 

My program uses multiple files, and I'd like to use this definition of verboseprint in all of them. All of the files will be passed the in_verbose_mode boolean. I know that I could just define verboseprint by itself in a file and then import it into all of my other files, but I need the function definition to be able to be declared two different ways based on a boolean.

So in summary: I need a function that can declare another function in two different ways, that I can then import into multiple files.

Any help would be appreciated.

2
  • You want to declare one function and used it everywhere in different ways, this is how polymorphism works: stackoverflow.com/questions/1031273/…. And usually, you don't define a function in python through this way. Commented Aug 3, 2016 at 17:37
  • I don't understand what you mean by "All of the files will be passed the in_verbose_mode boolean." do you mean each file has it's own variable like that? Commented Aug 3, 2016 at 17:44

2 Answers 2

3

You should look up the factory design pattern. It's basically designed to do exactly what you are talking about, though it would be with classes not functions. That being said, you can get the behavior that you want by having a class that returns one of two possible objects (based on your boolean). They both have the same method but it operates differently (just like your two functions).

Class A:
     def method():
          do things one way

Class B:
     def method():
          do things another way

     import A,B
Class Factory:
     def __init__(bool):
          self.printer = A if bool else B

     def do_thing():
          self.printer.method()




import Factory
fac = Factory(True)
fac.do_thing() # does A thing
fac = Factor(False)
fac.do_thing() # does B thing
Sign up to request clarification or add additional context in comments.

Comments

3

Usually you don't want define a function in this way.

And I think the easy way to achieve this is you pass the boolean as a function parameter and define the behavior based on the parameter:

def verboseprint (*args, mode):
    if mode == in_verbose_mode:
        for arg in args:
            print arg,
        print
        print "Done defining verbose print."
    # if we're not in verbosemode, do nothing
    ##else:
    ##    verboseprint = lambda *a: None 

And then import this function to use in your other files.

Comments

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.