Keep up with the passing of summary parameters of Python and learn python Parameters

Source: Internet
Author: User

Keep up with the passing of summary parameters of Python and learn python Parameters

As mentioned above, the basic content of the function has been completed. However, there are still a lot of interesting details about functions. It is described here.

Parameter transfer

In python, function parameters are assigned values to pass reference objects. The following is a summary of common function parameter definition methods to understand the process of parameter passing.

Def foo (p1, p2, p3 ,...)

This is the most common method. a finite number of parameters are listed and separated by commas. When calling a function, assign values to the parameters in order. Note that the parameter name is not important, but the location is important. In addition, the numbers must be consistent, one-to-one correspondence. The first object (which may be a value, String, etc.) corresponds to the first parameter, and the second parameter corresponds to the second parameter. Therefore, it must be left or right.
Copy codeThe Code is as follows:
>>> Def foo (p1, p2, p3 ):
... Print "p1 =>", p1
... Print "p2 =>", p2
... Print "p3 =>", p3
...
>>> Foo ("python", 1, ["qiwsir", "github", "io"]) # assign values one by one
P1 ==> python
P2 => 1
P3 => ['qiwsir ', 'github', 'io']

>>> Foo ("python ")
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: foo () takes exactly 3 arguments (1 given) # Check the error message.

>>> Foo ("python", 1, 2, 3)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: foo () takes exactly 3 arguments (4 given) # Three parameters are required. Four parameters are actually placed, and an error is returned.

Def foo (p1 = value1, p2 = value2 ,...)

This method makes the assignment of a parameter clearer than the previous one. It seems like this is not a mess. It is very clear. Quite a piece of radish means a pitfall.

Or the above function, assign values in the following way, so you don't have to worry about the order.
Copy codeThe Code is as follows:
>>> Foo (p3 = 3, p1 = 10, p2 = 222)
P1 => 10
P2 => 222
P3 => 3

You can also define parameters in the following way to give some parameters default values.
Copy codeThe Code is as follows:
>>> Def foo (p1, p2 = 22, p3 = 33): # The default values of p2 and p3 are set.
... Print "p1 =>", p1
... Print "p2 =>", p2
... Print "p3 =>", p3
...
>>> Foo (11) # p1 = 11. Other parameters are assigned by default.
P1 => 11
P2 ==> 22
P3 => 33
>>> Foo (11,222) # In order, p2 = 222, p3 still maintains the original default value
P1 => 11
P2 => 222
P3 => 33
>>> Foo (11,222,333) # assign values in sequence
P1 => 11
P2 => 222
P3 = & gt; 333

>>> Foo (11, p2 = 122)
P1 => 11
P2 => 122
P3 => 33

>>> Foo (p2 = 122) # p1 has no default value. values must be assigned. Otherwise, an error is returned.
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: foo () takes at least 1 argument (1 given)

Def foo (* args)

This method is suitable when you are not sure about the number of parameters, add a * Before The args parameter. Note that there is only one.
Copy codeThe Code is as follows:
>>> Def foo (* args): # receives uncertain data objects.
... Print args
...
>>> Foo ("qiwsir. github. io") # received in tuple format, even if it is
('Qiwsir. github. io ',)
>>> Foo ("qiwsir. github. io", "python ")
('Qiwsir. github. io ', 'python ')

An example is provided in the previous section, which can be used in combination with the previous section. I will not go into details here.

Def foo (** args)

The difference between this method and above is that you must receive a format similar to arg = val.
Copy codeThe Code is as follows:
>>> Def foo (** args): # receives data objects in the form of a dictionary.
... Print args
...

>>> Foo (1, 2, 3) # An error is returned.
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: foo () takes exactly 0 arguments (3 given)

>>> Foo (a = 1, B = 2, c = 3) # in this way, because of the key-Value Pair
{'A': 1, 'C': 3, 'B': 2}

Let's take a comprehensive look at the execution sequence of the above four parameter transfer methods.
Copy codeThe Code is as follows:
>>> Def foo (x, y = 2, * targs, ** dargs ):
... Print "x =>", x
... Print "y =>", y
... Print "targs_tuple =>", targs
... Print "dargs_dict =>", dargs
...

>>> Foo ("1x ")
X ==> 1x
Y => 2
Targs_tuple ==> ()
Dargs_dict ==> {}

>>> Foo ("1x", "2y ")
X ==> 1x
Y => 2y
Targs_tuple ==> ()
Dargs_dict ==> {}

>>> Foo ("1x", "2y", "3t1", "3t2 ")
X ==> 1x
Y => 2y
Targs_tuple ==> ('3t1', '3t2 ')
Dargs_dict ==> {}

>>> Foo ("1x", "2y", "3t1", "3t2", d1 = "4d1", d2 = "4d2 ")
X ==> 1x
Y => 2y
Targs_tuple ==> ('3t1', '3t2 ')
Dargs_dict ==>{ 'd2 ': '4d2', 'd1 ': '4d1 '}

Through the above example, do you see what name hall is?


How to Learn about Python

C ++, Java, and even C # can all be seen as the same type of language: C ++ is flexible, but complicated syntax makes production efficiency low, and Java improves production efficiency, however, flexibility is lost. C # is a good balance between production efficiency and flexibility, but it is still not enough. Otherwise, the father of Boo language will not be angry with Boo. Python is a dynamic type and a strong type language. A dynamic type means that you no longer need to make numerous declarations for the type of each variable, because the compiler will help you make type judgments, it will determine the type of the Variable Based on the Value assignment of the variable. A strong type means that you cannot use a string as an int unless you explicitly convert it. Python itself is compact, because space is ignored in C ++, Java, C #, and "{}" to define code blocks, if you like it, you can write all the code on one line, so you can write it as dizzy as possible. It is not possible, because there is only one Separator in it, namely the colon ":", and the code block is distinguished by indentation. Maybe you will be a bit unaccustomed to this method at the beginning, but later, you will find that this method will benefit you a lot, because you have developed a good code style. Don't think that Python is a very academic language, although many people think it is very suitable as an entry-level language for learning programming. In fact, Python is not only suitable for beginners to learn programming, but also a powerful language. You can use it to do anything other languages can do. Python itself is almost everywhere, and programs written in Python can run in various mainstream operating systems, even Palm. Oh, I almost forgot. Eric Raymond also told us that hackers must master four languages. The first step is Python ., Of course, if you cannot leave. NET in a day, you should start learning Python from IronPython. Compared with C ++, Java, and even C #, is it much faster to write and execute this classic program in Python? Haha ~~~ If I make up my mind to say that Python is highly productive, you will already scold me for being a lie. Okay. Let's try some practical code. But before that, you have to download two very famous class libraries: wxPython and Twisted. Is the download and Installation complete? Let's start our tour of EnjoyPythonwith you in ten minutes. FromwxPython. wximportwxPySimpleApp, wxFrameapp = wxPySimpleApp () frame = wxFrame (None,-1, "HelloWorld ")

What if people always cough?

Take medicine! Or go to the hospital to check the lung
 

Related Article

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.