Python function variable Lookup order

Source: Internet
Author: User

Python function variable Lookup order: Priority: Local---> Global---> If still not found, a nameerror error will be raised.

#!/usr/bin/env python#coding:utf-8# @Author: andy# Date:2017/6/14money = 1000def tell_info (name):p rint ("%s has%d"% (NA Me, Money)) def fun (): Money = 10tell_info (' Egon ') Fun () # Egon has 1000# tell_info function executed inside the fun function, but still needs to go back to the definition to find the relevant variable # Priority from the local find, the local not find the global, because the function meaning when the money=1000, and the execution position of the money=10, regardless. Money = 1000def F1 (): Money = 10def tell_inform (name):p rint ("%s With%d "% (name, money)) Tell_inform (' Egon ') F1 () # Egon has the 10# tell_inform function defined inside the F1, and there is a definition of the local variable, so the lookup variable value is found locally that stops # Will not be looked at globally, so money ends up with a 10 "" function scope is irrelevant to where the function is executed, only with respect to the definition function. Whichever call goes back to the definition phase to find the corresponding scope relationship. The lookup order is: local---> Global---> Nameerror ""

Python function variable Lookup order

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.