Passing parameters of python functions

Source: Internet
Author: User
In this case, we will discuss the parameter passing problem of the function. The python version I use is 3.3.2 for the function: in this case, we want to discuss the parameter passing problem of the function.

The python version I use is 3.3.2.

For functions:

1

2

3

4

5

6

7

8

Def fun (arg ):

Print (arg)

Def main ():

Fun ('Hello, Hongten ')

If _ name _ = '_ main __':

Main ()

When we pass a parameter to the fun () function, we can print the passed parameter value information.

The printed information here is:

Hello, Hongten

For the following use cases:

1

2

3

4

5

6

7

8

9

Def fun (a = 1, B = None, c = None, * args ):

Print ('{0}, {1}, {2}, {3}'. format (a, B, c, args ))

Def main ():

Fun (a = 'one ')

Fun ('one ')

If _ name _ = '_ main __':

Main ()

When the passing parameters are fun (a = 'one') and fun ('one'), the values are copied to parameter, the effects of all two parameters are the same:

One, None, None ,()

One, None, None ,()

Of course, we can also assign values to parameters B, c, and * args.

For example:

1

2

3

4

5

6

7

8

Def fun (a = 1, B = None, c = None, * args ):

Print ('{0}, {1}, {2}, {3}'. format (a, B, c, args ))

Def main ():

Fun ('one', 1, 2, ('hongten '))

If _ name _ = '_ main __':

Main ()

In this way, values are assigned to parameters B, c, and args.

Running effect:

One, 1, 2, ('hongten ',)

In the preceding column, we cannot bypass the parameters a, B, and c in front of * args to copy * args:

For example:

def fun(a=1, b=None, c=None, *args):    print('{0},{1},{2},{3}'.format(a, b, c, args))      def main():    fun(args=('hongten'))      if __name__ == '__main__':    main()

Running effect:

Traceback (most recent call last ):

File "E:/Python33/python_workspace/test_fun.py", line 21, in

Main ()

File "E:/Python33/python_workspace/test_fun.py", line 18, in main

Fun (args = ('hongten '))

TypeError: fun () got an unexpected keyword argument 'args'

However, for parameters a, B, and c, you can assign values in this way.

For example:

def fun(a=1, b=None, c=None, *args):    print('{0},{1},{2},{3}'.format(a, b, c, args))      def main():    fun(c=('hongten'), b=2, a=[1,2,3])      if __name__ == '__main__':    main()

Running effect:

[1, 2, 3], 2, hongten ,()

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.