With the old Ziko python variables and parameters _python

Source: Internet
Author: User
Tags in python

So what are the differences and connections between the two? I searched the internet, and found that many of the statements, although the same, but it seems only the following section from the Microsoft site is highly abstract, and far-reaching significance. I excerpt over, reader read, whether understand, although it is for VB, the same inspiration.

Copy Code code as follows:

Differences between parameters and Variables (visual Basic)

In most cases, the procedure must contain some information about the calling environment. The process of performing a duplicate or shared task uses different information for each invocation. This information contains the variables, constants, and expressions that are passed to it each time the procedure is invoked.

To pass this information to a procedure, the procedure first defines a formal parameter, and then the calling code passes an argument to the defined formal parameter. You can treat a formal parameter as a parking space and use the argument as a car. Just as a parking space can park different cars at different times, the calling code can pass different arguments to the same parameter each time the procedure is invoked.

A parameter represents a value that the procedure expects you to pass when calling it.

When you define a Function or Sub procedure, you need to specify the parameter list in parentheses immediately following the procedure name. For each parameter, you can specify a name, data type, and an incoming mechanism (ByVal (Visual Basic) or BYREF (Visual Basic)). You can also indicate that a formal parameter is optional. This means that the calling code does not have to pass its value.

The name of each parameter can be used as a local variable within the procedure. The parameter name is used in the same way as any other variable.

An argument represents a value that is passed to a procedure parameter when you call a procedure. The calling code provides arguments when the procedure is invoked.

When you call a Function or Sub procedure, you need to include the argument list in parentheses immediately following the procedure name. Each argument corresponds to that parameter in the same position in this list.

Unlike formal parameter definitions, arguments do not have a name. Each argument is an expression that contains 0 or more variables, constants, and text. The data type of the evaluated expression should usually match the data type defined for the corresponding parameter, and in any case the expression value must be convertible to this parameter type.

Reader if the bite to read this quote, found that there are several keywords: parameters, variables, formal parameters, arguments. Originally wanted to figure out the parameters and variables, the result of another two things, more confusing. Please a little Ann not impatient, originally this quote is a bit superfluous, but, the reason is to let you broaden your horizons, in the programming industry, similar things have many nouns. The next time you hear someone say that, don't be afraid, you've heard it anyway.

In Python, it's not so complicated.

After reading the above confusing citation, look at the following code, it will be enlightened.

Copy Code code as follows:

>>> def Add (x): #x是参数
.. A = #a是变量
... return a+x
...
>>> x = 3 #x是变量, just outside the function
>>> Add (x) #这里的x是参数, but it is passed object 3 by the preceding variable x
13
>>> Add (3) #把上面的过程合并了
13

At this point, whether reader clear a little bit. Of course, I expressed the wrong place or understand the mistake, also please reader generous enlighten, can be zuoyi thanks.

Global variables and local variables

Here is a piece of code, note that there is a function in this code FUNCX (), this function has a variable x=9, in front of the function also has a variable x=2

Copy Code code as follows:

x = 2

Def funcx ():
x = 9
Print "This x are in the funcx:-->", X

Funcx ()
Print "--------------------------"
Print "This x was out of funcx:-->", X

So, what is the result of this code output? See:

Copy Code code as follows:

This x is in the Funcx:--> 9
--------------------------
This x is out of funcx:--> 2

From the output, run Funcx (), output the variable x=9 in the Funcx (), and then execute the last line in the code, print "This x was out of the funcx:-->", X

It is particularly concerned that the previous x outputs the variable x within the function, and the last x outputs the variable x outside the function. Two variables don't interact with each other, though they are all x. It is seen from here that two x each play a role in their respective fields, then such variables are called local variables.

There are local, there is a corresponding all, in Chinese, all variables, seems to have ambiguity, thanks to Chinese rich, and then took a noun: global variable

Copy Code code as follows:

x = 2
Def funcx ():
Global X
x = 9
Print "This x are in the funcx:-->", X

Funcx ()
Print "--------------------------"
Print "This x was out of funcx:-->", X

The difference between the two pieces of code is that the latter has a global X in the function, which means declaring that x is a global variable, that is, the X is the same as the one outside the function, and then the reference object of x is changed to 9 by x=9. So, there is the following result.
This x is in the Funcx:--> 9
--------------------------
This x is out of funcx:--> 9

As if the global variable ability is very strong, can command the function inside and outside. However, be aware that this thing should be used carefully, because it is often easy to bring variable changes in the chaos. Inside and outside different, must notice in the procedure.

Number of indeterminate arguments

When designing a function, sometimes we can confirm the number of parameters, such as a function to compute the area of a circle, which requires a radius (πr^2), and the parameter of the function is OK.

However, the world is not always so simple, and not always so sure, but uncertainty is the world often exist. If reader understand quantum mechanics, a lot of people have heard nothing, then understand the real uncertainty. Of course, without studying quantum mechanics, the world is full of uncertainties, as can be realized. Isn't it? Is it not a blessing in disguise?

Since there is a lot of uncertainty, then the number of parameters of the function, of course, there is uncertainty, the function of how to solve this problem? Python solves the uncertainty of the number of parameters in this way:

Copy Code code as follows:

def add (X,*arg):
Print x #输出参数x的值
result = X
Print arg #输出通过 value obtained by *arg method
For I in ARG:
Result +=i
return result

Print Add (1,2,3,4,5,6,7,8,9) #赋给函数的参数个数不仅仅是2个

After running this code, you get the following results:
1 #这是函数体内的第一个print, the value of parameter x is 1.
(2, 3, 4, 5, 6, 7, 8, 9) #这是函数内的第二个print, parameter Arg gets a tuple
#最后的计算结果

The result of this output is quite unfriendly, and if you do not follow the original function, you do not know what each line prints. Blame yourself.

As you can see from the example above, if you enter too many parameters, all other parameters are passed through *arg and passed to the parameter (variable) arg in the tuple form. Please reader note that I have used a vague word here: the argument (variable), which means that, before the data is passed in, ARG is the parameter in the function's head, and it is used in the function statement, which is the variable. That is, in many cases, the parameters and variables in the function are not so distinguished, as long as you know the object is through what channel, that thing to what the target can be.

To make it more obvious that the args (name can be different, but the symbol must have), you can use one of the following simple functions to demonstrate:

Copy Code code as follows:

>>> def foo (*args):
.. print args #打印通过这个参数得到的对象
...
>>> #下面演示分别传入不同的值, the results obtained by parameter *args

>>> foo (1,2,3)
(1, 2, 3)

>>> foo ("Qiwsir", "Qiwsir.github.io", "Python")
(' Qiwsir ', ' qiwsir.github.io ', ' python ')

>>> foo ("Qiwsir", 307,["Qiwsir", 2],{"name": "Qiwsir", "Lang": "Python"})
(' Qiwsir ', 307, [' Qiwsir ', 2], {' lang ': ' Python ', ' name ': ' Qiwsir '})

Whatever it was, it was crammed into the tuple.

In addition to receiving multiple values in this form of *args, you can also receive values in the form of **kargs, but this time it's a bit different:

Copy Code code as follows:

>>> def foo (**kargs):
... print Kargs
...
>>> foo (a=1,b=2,c=3) #注意观察这次赋值的方式和打印的结果
{' A ': 1, ' C ': 3, ' B ': 2}

What would be the result if you were to use Foo (1,2,3) this time?

Copy Code code as follows:

>>> foo (1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Typeerror:foo () takes exactly 0 arguments (3 given)

Reader here may have thought, is not the uncertainty? I do not know what kind of parameters will be the way to pass the value ah, this is good to do, the above are integrated.

Copy Code code as follows:

>>> def foo (X,y,z,*args,**kargs):
... print X
... print y
... print Z
... print args
... print Kargs
...
>>> foo (' Qiwsir ', 2, "Python")
Qiwsir
2
Python
()
{}
>>> foo (1,2,3,4,5)
1
2
3
(4, 5)
{}
>>> foo (1,2,3,4,5,name= "Qiwsir")
1
2
3
(4, 5)
{' name ': ' Qiwsir '}

It's good, so it's enough to handle a variety of parameter requirements.

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.