Re: [Tutor] Passing functions as arguments to other functions
boB Stepp writes: > So the impression I am getting is that a function name by itself (with > no parentheses) is the function *object*. Yes. The expression ‘func’ resolves to whatever object is referenced by that name; if it's a function object, you get that object. The expression ‘func(foo, bar)’ resolves to whatever object is returned from *calling* the object ‘func’, with the arguments ‘foo, bar’. > But why does Python require separating the function object from its > parameters when it is being passed as an argument to another function? You've already answered your question. An expression resolves to exactly one object. Either you want the function object, or you want something else. In the case of ‘func(foo, bar)’ you will get exactly one object from that expression. You won't get the ‘func’ object as well; you need a different expression (the expression ‘func’) to get that. If you want to talk about different objects at the same time, you need multiple parameters in which to place those objects. -- \ “By instructing students how to learn, unlearn, and relearn, a | `\ powerful new dimension can be added to education.” —Alvin | _o__)Toffler, _Future Shock_, 1970 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Passing functions as arguments to other functions
I believe I understand the barebone mechanics on how to do this. But I do not understand the rationale of why Python does it the way it does. Say def f(g, *args): g(*args) def g(*args): # Do something. do_things = f(g, *args) is the outline of how I understand the mechanics of doing this. But noob boB initially want to do instead: def f(g(*args)): g(*args) def g(*args): # Do somenthing. do_things = f(g(*args)) which, of course, will give me a syntax error. Also, I note that if I just type a function name without the parentheses in the interpreter, I will get something like this: >>> def f(): pass >>> f So the impression I am getting is that a function name by itself (with no parentheses) is the function *object*. But why does Python require separating the function object from its parameters when it is being passed as an argument to another function? -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Testing print
Testing output of print functions (Py 3). First off, is it worth it to do so? Second, it seems that prints are often intermingled with the main logic of a function and only serve to pass on a message to the user. For example, in an earlier thread ( Questions as to how to run the same unit test multiple times on varying input data.: https://mail.python.org/pipermail/tutor/2016-September/109686.html) I finally wound up with this function: def right_justify(a_string): '''This fucntion will take the string, "a_string", and right justify it by padding it with spaces until its last character falls in column 70 of the display. The padded string will be returned.''' if len(a_string) <= 70: num_pad_spcs = 70 - len(a_string) return (' ' * num_pad_spcs) + a_string else: print("The string has too many characters (> 70)!") print("Only a partial, 70 character line will be returned.") return a_string[:70] I had to consider the possibility that the string supplied to the function was more than 70 characters long. The exercise in "Think Python2" did not give any input into how to handle such a case, so I decided I would return 70 characters worth of the overly long string and print a message to the user. So (Even though this is a simple exercise.) should I test the prints? And if so, how to I separate this out? In my mind, if I am going to be testing calls to print, then perhaps I should write a separate function, say "print_msg(msg)" and put that in place of the prints in the else clause and then test the print_msg function, where "print(msg)" would be isolated in its own little function to be tested.. Anyway, it would seem that the only way to capture the output of a print is to redirect the stdout to something I can capture and compare against. Googling brings up some people doing something with mock objects, some redirecting to a string buffer, some to a file, some to other things. What, in your opinion(s), is the cleanest way to handle this? -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor