#๐Ÿ”’ How to have an arbitrary amount of arguments and then an optional one in a function?

43 messages ยท Page 1 of 1 (latest)

icy schooner
#

Was trying to write a command that puts anything printed to the console into a log file for later observation (but ended up finding this online anyway)

as shown in my example below, is there any way to have message_to_print defined as any number of arguments given, and for example, log_file to only accept a command if it ends in ".txt"?
i know i could just make every print command use fstrings, but that would involve reformatting like 50 print commands, which i could do, but this answer seems both good to know and is something ive wondered for a bit. i know *args exists but im unsure how to apply it, if thats the answer

def ConsolePrint(message_to_print, log_file='ConsoleOutput.txt'):
    print(message_to_print)
    with open(log_file, 'a') as of:
        of.write('\n' + message_to_print)


ConsolePrint("This is Part 1", "and this is Part 2,", "and this is part 3.", "TestOutput.txt")```
^Currently the `ConsolePrint` command thinks "and this is part 2," is the logfile, and everything else is considered nonsense
late fiberBOT
#

@icy schooner

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

median hill
#

I assume you know about *args and **kwargs?

icy schooner
#

i know in the most basic sense

#

but like i said i am unsure how to apply that knowledge

#

and i only barely understand that there is a dif, but not what it is

vocal flame
quiet bloom
median hill
#

You can also have a bare * in your list of arguments, which just means "all arguments after this must be keyed when you call this function"

icy schooner
quiet bloom
#

it basically lets you put how many arguments you want inside a function just from one argument

#

def console_print(*messages, log_file="ConsoleOutput.txt", sep=" ", end="\n"):

#
console_print("This is Part 1",
              "and this is Part 2,",
              "and this is Part 3.",
              log_file="TestOutput.txt")```
#

*messages this is basically saying: put as many arguments as you want, which will make messages become a tuple

#

(not sure if its a tuple or array)

quiet bloom
median hill
quiet bloom
#

ah yeah you're right

median hill
median hill
median hill
icy schooner
#

yeah ive been tryna put this info together mentally to rewrite it

#

gimme juuust a sec

icy schooner
icy schooner
#

that is, the first consoleprint would just do it to ConsoleOutput.txt, but the second would do it to TestConsole.txt

#

pogging

#

this is good ok cool

#

like i said, i know i could have just used the original version and done all prints in one big fstring, but i felt this was a good op to learn

#

and now i think i undastand args and kwargs better yay moment

quiet bloom
#

yeah it always is! Way less boring boilerplate to write

median hill
#

Best way*

#

You can technically do everything with a relatively small set of features

icy schooner
#

yeah but whats the point if youre not gonna be a little extra

#

flex that muscle

#

convenience. anyway starts breakdancing

#

how do i close this

#

!close

late fiberBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.