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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
CdSdw
  • 349
  • 1
  • 6
  • 19
  • You want to declare one function and used it everywhere in different ways, this is how polymorphism works: http://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used. And usually, you don't define a function in python through this way. – RandomEli Aug 03 '16 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? – Tadhg McDonald-Jensen Aug 03 '16 at 17:44

2 Answers2

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.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
RandomEli
  • 1,527
  • 5
  • 30
  • 53
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
bravosierra99
  • 1,331
  • 11
  • 23