Python *args, **kwargs questions

Asked 1 years ago, Updated 1 years ago, 306 views

def custum_print(value, *args, **kwargs):
    print(value, *args, **kwargs)

custum_print(1, 2, 3, sep=':', end=' ')`

In the above code, it is said that it is correct not to attach the whole * to *args and **kwargs in print(). But I don't really understand how it works when you write code like that. The above code output value is 1:2:3.

1 is value, 2,3 is args, whether this is how it is recognized, If so, sep=':'end=' should be recognized as dictionary, but it doesn't print out, and it's also interesting to see 1:2:3 if it's not recognized when * is attached.

For your information..

def custum_print(value, *args, **kwargs):
    print(value, args, kwargs)

custum_print(1, 2, 3, sep=':', end='')

takes off * 1 (2,3) {sep: ':', end:} It came out like this.

It's a peripheral question, but I'm just wondering how it works.

python

2023-06-01 20:51

1 Answers

If you print the factors yourself, or run them in debug mode in vscode, I think you can explore more of your curiosity.

I checked with vscode debug mode (of course, you can check the value and type of each factor with print statements, etc.), but if you can write a debugger, let's use a debugger.)

The value of the factors in the custom_print is

That's how it went in like this.

"It's right not to put a *" doesn't always sound right, but it depends on how you use it.

If you're passionate about posting this much questions, make sure to check it out yourself in debug mode.


2023-06-02 13:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.