Local, global, free variables in Python

The Execution Model of Python (in python doc) says

If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.

Actually "global variables are a subset of free variables". The CPython implementation detail is if CPython marks a variable as "free", it means the marked variable is pure "free" but not "global" (in turn it means if a variable is free and global, is_free in CPython will return false).

CPython symtable.c module, SET_SCOPE says "An implicit  global is a free variable for which the compiler has found no binding in an enclosing function scope"

Reference:
http://www.gossamer-threads.com/lists/python/dev/860658?do=post_view_flat#860658
http://stackoverflow.com/questions/12919278/how-to-define-free-variable-in-python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值