LPTHW Stupid Methodology Python Chapter 18

Source: Internet
Author: User

After reading chapter 18, I found that the first exercise, using *args to read all of the input parameters as a tuple, but in his practice only gave two variables to assign value, when the user is not clear about the definition of this function, it is possible to give too many variables into this function, then the following error occurs:

Valueerror:too Many values to unpack

So-called unpacking errors, too many values are given.

So in order to stop this situation, I trickery solved the next. Is that I changed the tuple to a list, and then made up 2 bits to go in.

It doesn't matter if he gives less variables, I can do it. may be too rubbing, and later have a good idea to make up.

def print_two (*args):
All_args = List (args) + [None,none] = all_args[0:2]
= Need_var print"arg1:%r, arg2:%r" %(ARG1,ARG2) Print_ Both (1,"hello", 3)

LPTHW Stupid Methodology Python Chapter 18

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.