Python script function passing parameters

Source: Internet
Author: User
Tags python script

For example, this code:
def add (num):
num= num+10
d=2
Add (d)
Print D

Output 2
What should I do if I want to pass the address of D as C so that it outputs 12?

The function of Python is assigned, and the assignment is implemented by establishing a variable with the object's association.

    • Executing d = 2 o'clock, you create D in __main__ and point it at 2 for this integer object.

    • Execute the function in the Add (d) procedure:

      • After D is passed to the Add () function, Num also points to the 2 in __main__ within the function.

      • After executing num = num + 10, new Object 12 is created, and Num points to the new object,--12.

    • If you understand the difference between a local variable in a function and a variable in a __main__, it is clear that in __main__, D is still pointing to the object 2, which does not change. So, you get 2 when you print D.

If you want to make the output 12, the simplest way is:

    1. Add return num to function add ()

    2. Use D = add when calling function (d)


The code is as follows:

def add (num): num + = ten return NUMD = 2d = Add (d) Print D



October yards
Links: https://www.zhihu.com/question/20591688/answer/24372602
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.



This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1917379

Python script function passing parameters

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.