Python print() question
John Rickman (71) 646 posts |
The python print command supports a key parameter for suppressing lf/cr so that consecutive print statements print to the same line; as shown in the first snippet of code.
prints this:
I would like to pass a string to a function and have it print to the same line or not according to the presence or otherwise of the key parameter. Unfortunately the print command does not recognize the key parameter if it is embedded in a passed string. See below for example
prints this:
I have tried a variety of variations including str(), and eval() and am currently looking into exec() but so far with no success. |
Matthew Phillips (473) 721 posts |
I am not quite sure what you are trying to do. The named parameter called “end” is a parameter, not part of the string to be printed, so it is not surprising that it does not work embedded in the string. Your example
is effectively the same as
so you should expect that result. The named parameter “end” has to be a separate parameter to the print function. As far as I understand your question, you want to be able to pass a string to a function, and have it print a newline at the end, or not, depending on some criterion? What about
Then you can do
The “same_line” parameter would be optional and defaults to False. (Not tested.) If that is not what you are trying to do, could you explain more fully? |
Dave Higton (1515) 3534 posts |
Tested :-) |
David J. Ruck (33) 1636 posts |
Another solution is to have all your prints use end=’’ and pass the newlines explicitly in the strings where needed.
|
John Rickman (71) 646 posts |
Having reread my post I can see it could have been better framed. Sorry for that. What I want to do is to pass the print parameters to a function and have the function print the passed string and if present respect any key parameters. Something like this:
I hoped to persuade the print object to accept a variable containing a keyword pair. |
Matthew Phillips (473) 721 posts |
So you want to create a function, e.g., “cat_print” which can take a variable number of unnamed arguments which will be concatenated into a single string. And that string will then be printed by the normal “print” function with any named arguments to cat_print passed through to print unaltered? Firstly, have you tried
Because setting “sep” (the separator) will stop the arguments being separated by spaces, if that is what you are wanting. At the moment your proposed cat_print is pretty much the same as print with sep=’’. |
John Rickman (71) 646 posts |
Yes that sums it up nicely. The print needs to be in a function because it logs to a file depending on the state of a debug flag. I don’t want to clutter my code with conditional debug stuff. So the problem is how to execute a print statement in a function using passed parameters. There is no problem with the strings, but I don’t know how to send keyword args to print() short of generating the print() statement on-the-fly and then using exec() to run it. |
Charles Ferguson (8243) 427 posts |
Understand that this is a Python-specific question, which has no RISC OS-dependant components in it. As such, there is a wealth of information on the web about how to handle keyword arguments – searching for ‘how to pass keyword arguments in python’ will give you a selection of answers. Try https://stackoverflow.com/questions/42724242/how-can-i-pass-keyword-arguments-as-parameters-to-a-function for an explicit answer. However, to answer the question directly… You’ve already understood that you need to gather the keyword arguments in a kwargs parameter. When you use **kwargs in the function arguments, this populates kwargs with the keyword arguments as a dictionary. You can see this if you print the name of the class. This means you can manipulate those keyword parameters just as you would any other dictionary. To see this: >>> def foo(**kwargs): ... print("kwargs is a {}".format(kwargs.__class__.__name__)) ... >>> foo(mykeyword=1) kwargs is a dict If I’ve understood correctly you’re just wanting to take the non-keyword arguments that you’ve been given and print them, with similar semantics to the print function’s keyword arguments. Matthew’s given you a perfect way to do it with the separator. But let’s just say that you want to make the You might do it like this: def cat_print(*args, **kwargs): kwargs['sep'] = '' print(*args, **kwargs) When the When you call a function with a tuple prefixed by a In the same way, if you call a function with a dictionary prefixed by a Between the function entry and the call of the function, we can change the arguments (or we could even create a whole other dictionary to use). In this case, the At the command line: >>> def cat_print(*args, **kwargs): ... kwargs['sep'] = '' ... print(*args, **kwargs) ... >>> cat_print("a", "b", "c") abc >>> cat_print("a", "b", "c", end="<EOL>") abc<EOL>>>> |
Matthew Phillips (473) 721 posts |
It would have been really helpful to have had that little piece of information at the outset, because it would have very much helped us understand what you were actually aiming for! Thank you to Charles for the very helpful reply. I had been going to write a bit more, but I was having trouble Googling to find out how to pass |
John Rickman (71) 646 posts |
Thanks Charles I will study your reply and implement it as best I can. Thanks Matthew also. with regard to:
I did say in the second sentence in the original post:
|
Matthew Phillips (473) 721 posts |
I know you said that, but as you did not explain the purpose of the function, it was harder to understand why you wanted to do this, rather than just use print direct. I know that your description was literally correct, but a bit more context often helps. There might have been a really excellent logging function that someone might have recommended instead, for example. (There are several Python logging tools.) We got there in the end, anyway! |
David J. Ruck (33) 1636 posts |
If you are logging to a file from Python use the standard logging module, rather than reinventing the wheel. |