split() returns substrings, not integers. So if you do a,b,c = '4 5 6'.split(), a will be a string '4', b will be a string '5', and c '6'. Then if you 'add' a and b, they will be concatenated as '45'
Absolutely, I have noticed too that he made a mistake. Split() is a string method and it returns a list of substrings. So each value will have to be of the string class and not of the int class.
When you are using **kwargs for a parameter, you are telling it that what you are giving it is going to be packed into a dictionary. It's not expecting a dictionary, it's expecting keys and values with which it will create a dictionary. So, the reason it doesn't work is that isn't what is being expected. However, you CAN use the dictionary syntax, if you the ** operator to unpack a dictionary into names and values, which then would be repacked in the function. The following is legal syntax: func(**{'a':100, 'b':100})
you will not be able to do calculate on split return elements , because it returns strings "sub_strings" . do type(a or b or c ) . do int(a or b or c ) first , man focus . good job .
@@oggiai I hate to be the bearer of bad news , but, Python is almost as idiosyncratic and weird as Perl is. I think I'll stick to C/C++ , at least it makes sense consistently !
it's just for passing an unknown number of key-value pairs to a function. There are other ways to do that (simply passing a dict), so I agree **kwargs is not extremely useful. But as a programmer you often need to figure out how other peoples' code works, so you should learn these nuances of the language.
There are many uses, but one of the most common is simply if you want to have lots of options when calling a function but you don't want to have make a big long list of function parameters for all possible options, especially when most of the time you only need a small number of required parameters. For example, if you have a function that sorts list, you could have additional options such as whether you want it in reverse sorted, or just the first n values, or some other feature. You could make those additional options supported by **kwargs. If you don't need them, they can be left out entirely. If you don't include them in the function call, it doesn't throw an error and just makes a zero length dictionary. When you do want those additional options, just include the ones you want.
and I have to add, I strongly disagree about it being "not extremely useful". A lot of the built in functions of Python use this very feature. And understanding it isn't just for understanding other people's code, but for understanding a lot of the official documentation. I for one have written a lot of pieces of code that were a bit dodgy with additional arguments that were only needed in some special cases that I later realized, "I should have just used **kwargs for that".
@@flioink Yes, it does require a bit of additional work when creating the function, but it provides you with more flexibility when calling it. Another common use-case for it is if you are creating a command line programs. A typical command line program often has standard use cases that involve a small number of options, but has large numbers of optional options and parameters that can be used. If you ever use command line programs then you should be very familiar with the pattern, and this is how you can implement it.