Python function default argument for variable object understanding

Source: Internet
Author: User
Tags function definition

1. During the execution of the code, the function definition is encountered, and the initialization function generates a function object that stores the function name, the default parameter value, and the function address.

2. Code execution is not in the initialization function, but directly executes the function body.

Code instance

This is to say from the feature of the function, in Python, the function is the first class of objects (functions is the object), in other words, the function is also an object, as an integer, a string can be assigned to a variable, as a parameter passed, can also be used as the return value. Functions also have their own properties, such as the name of the function, the default argument list of the function.

# The name of the function

>>> func.__name__

' Func '

# default parameter list for the function

>>> func.__defaults__

([1, 1, 1, 1, 1], 1)

DEF is an executable statement, when the Python interpreter executes the DEF statement, it creates a function object in memory (at this point, the code logic inside the function does not execute, because it is not called), in the global namespace, a function name (variable called func) points to the function object, remember, From start to finish, no matter how many times the function is called, there is only one function object, which is function object, and does not appear multiple function objects because it is called multiple times.

After the function object is generated, its properties: the name and default argument list are initialized to complete.

When initialization is complete, the first default parameter in property __default__ numbers points to an empty list.

When the function is first called, it is the first time that func () is executed, the logic code inside the function starts executing (the function no longer needs to be initialized), and the code logic is to add an element with a value of 1 to numbers.

Call Func () for the second time to continue adding an element to the numbers

Third time, four times and so on.

So now you should understand why the same function is called and the return value is different every time. Because they are sharing the same list (numbers) object, just adding an element to the list once per call

If we specify the numbers parameter, the result is very different.

>>> func (numbers=[10, 11])

[10, 11, 1]

Because numbers is re-assigned, it no longer points to the list that was originally initialized, but instead points to the new list object we passed in the past, so the return value becomes [10, 11, 1]

So how do we avoid the previous situation? is not to use a Mutable object as the default value for the parameter.

The right way:

>>> Deffunc (Numbers=none, num=1):

... ifnumbers isnone:

... numbers = [num]

.. else:

... numbers.append (num)

... returnnumbers

...

Python function default argument for variable object understanding

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.