__init__ raises an exception, then __del__ will still be called del x 并不直接调用 x.__del__() --- 前者会将 x 的引用计数减一,而后者仅会在 x 的引用计数变为零时被调用。 内存

 

7.13. nonlocal 语句

nonlocal_stmt ::=  "nonlocal" identifier ("," identifier)*

nonlocal 语句会使得所列出的名称指向之前在最近的包含作用域中绑定的除全局变量以外的变量。 这种功能很重要,因为绑定的默认行为是先搜索局部命名空间。 这个语句允许被封装的代码重新绑定局部作用域以外且非全局(模块)作用域当中的变量。

与 global 语句中列出的名称不同,nonlocal 语句中列出的名称必须指向之前存在于包含作用域之中的绑定(在这个应当用来创建新绑定的作用域不能被无歧义地确定)。

nonlocal 语句中列出的名称不得与之前存在于局部作用域中的绑定相冲突。

 

 

9.1. 名称和对象

对象之间相互独立,多个名称(在多个作用域内)可以绑定到同一个对象。 其他语言称之为别名。Python 初学者通常不容易理解这个概念,处理数字、字符串、元组等不可变基本类型时,可以不必理会。 但是,对涉及可变对象,如列表、字典等大多数其他类型的 Python 代码的语义,别名可能会产生意料之外的效果。这样做,通常是为了让程序受益,因为别名在某些方面就像指针。例如,传递对象的代价很小,因为实现只传递一个指针;如果函数修改了作为参数传递的对象,调用者就可以看到更改 --- 无需 Pascal 用两个不同参数的传递机制。

9.2. Python 作用域和命名空间

在介绍类前,首先要介绍 Python 的作用域规则。类定义对命名空间有一些巧妙的技巧,了解作用域和命名空间的工作机制有利于加强对类的理解。并且,即便对于高级 Python 程序员,这方面的知识也很有用。

接下来,我们先了解一些定义。

namespace (命名空间)是映射到对象的名称。现在,大多数命名空间都使用 Python 字典实现,但除非涉及到优化性能,我们一般不会关注这方面的事情,而且将来也可能会改变这种方式。命名空间的几个常见示例: abs() 函数、内置异常等的内置函数集合;模块中的全局名称;函数调用中的局部名称。对象的属性集合也算是一种命名空间。关于命名空间的一个重要知识点是,不同命名空间中的名称之间绝对没有关系;例如,两个不同的模块都可以定义 maximize 函数,且不会造成混淆。用户使用函数时必须要在函数名前面附加上模块名。

点号之后的名称是 属性。例如,表达式 z.real 中,real 是对象 z 的属性。严格来说,对模块中名称的引用是属性引用:表达式 modname.funcname 中,modname 是模块对象,funcname 是模块的属性。模块属性和模块中定义的全局名称之间存在直接的映射:它们共享相同的命名空间! 1

属性可以是只读或者可写的。如果可写,则可对属性赋值。模块属性是可写时,可以使用 modname.the_answer 42 。del 语句可以删除可写属性。例如, del modname.the_answer 会删除 modname 对象中的 the_answer 属性。

命名空间是在不同时刻创建的,且拥有不同的生命周期。内置名称的命名空间是在 Python 解释器启动时创建的,永远不会被删除。模块的全局命名空间在读取模块定义时创建;通常,模块的命名空间也会持续到解释器退出。从脚本文件读取或交互式读取的,由解释器顶层调用执行的语句是 __main__ 模块调用的一部分,也拥有自己的全局命名空间。内置名称实际上也在模块里,即 builtins 。

函数的本地命名空间在调用该函数时创建,并在函数返回或抛出不在函数内部处理的错误时被删除。 (实际上,用“遗忘”来描述实际发生的情况会更好一些。) 当然,每次递归调用都会有自己的本地命名空间。

作用域 是命名空间可直接访问的 Python 程序的文本区域。 “可直接访问” 的意思是,对名称的非限定引用会在命名空间中查找名称。

作用域虽然是静态确定的,但会被动态使用。执行期间的任何时刻,都会有 3 或 4 个命名空间可被直接访问的嵌套作用域:

  • 最内层作用域,包含局部名称,并首先在其中进行搜索

  • the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contain non-local, but also non-global names

  • 倒数第二个作用域,包含当前模块的全局名称

  • 最外层的作用域,包含内置名称的命名空间,最后搜索

If a name is declared global, then all references and assignments go directly to the next-to-last scope containing the module's global names. To rebind variables found outside of the innermost scope, the nonlocal statement can be used; if not declared nonlocal, those variables are read-only (an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged).

通常,当前局部作用域将(按字面文本)引用当前函数的局部名称。在函数之外,局部作用域引用与全局作用域一致的命名空间:模块的命名空间。 类定义在局部命名空间内再放置另一个命名空间。

划重点,作用域是按字面文本确定的:模块内定义的函数的全局作用域就是该模块的命名空间,无论该函数从什么地方或以什么别名被调用。另一方面,实际的名称搜索是在运行时动态完成的。但是,Python 正在朝着“编译时静态名称解析”的方向发展,因此不要过于依赖动态名称解析!(局部变量已经是被静态确定了。)

Python 有一个特殊规定。如果不存在生效的 global 或 nonlocal 语句,则对名称的赋值总是会进入最内层作用域。赋值不会复制数据,只是将名称绑定到对象。删除也是如此:语句 del x 从局部作用域引用的命名空间中移除对 x 的绑定。所有引入新名称的操作都是使用局部作用域:尤其是 import 语句和函数定义会在局部作用域中绑定模块或函数名称。

global 语句用于表明特定变量在全局作用域里,并应在全局作用域中重新绑定;nonlocal 语句表明特定变量在外层作用域中,并应在外层作用域中重新绑定。

9.2.1. 作用域和命名空间示例

下例演示了如何引用不同作用域和名称空间,以及 global 和 nonlocal 对变量绑定的影响:

def scope_test():
    def do_local():
        spam = "local spam"

    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"

    def do_global():
        global spam
        spam = "global spam"

    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

示例代码的输出是:

After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam

注意,局部 赋值(这是默认状态)不会改变 scope_test 对 spam 的绑定。 nonlocal 赋值会改变 scope_test 对 spam 的绑定,而 global 赋值会改变模块层级的绑定。

而且,global 赋值前没有 spam 的绑定。

 

9. 类 — Python 3.11.3 文档 https://docs.python.org/zh-cn/3/tutorial/classes.html

7. 简单语句 — Python 3.11.3 文档 https://docs.python.org/zh-cn/3/reference/simple_stmts.html#nonlocal

 

 

del x 并不直接调用 x.__del__() --- 前者会将 x 的引用计数减一,而后者仅会在 x 的引用计数变为零时被调用。

3. 数据模型 — Python 3.11.3 文档 https://docs.python.org/zh-cn/3/reference/datamodel.html#types

 

 

issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org/issue808164

 3. Data model — Python 3.8.3rc1 documentation https://docs.python.org/3/reference/datamodel.html#object.__del__

 

Re: __init__ and __del__

Guido.van.Rossum@cwi.nl
Tue, 18 May 1993 16:32:23 +0200

 

>From the responses so far I can conclude that having __init__
explicitly call the base class's __init__ is the best possible
solution, so let's move on to Chris' suggestion:

> By the way, will there be a __del__(self) method that is called at
> destruction time? My thought on this was that when python sees that an
> object should be garbage collected, it calls the object's __del__
> method (if any) before actually destroying it. Of course the
> interpreter would have to check the object's refcount after calling
> the function, as the method may have caused the object to be
> referenced by some other object.

The problem with this is, what to do if the __del__ call creates
another reference to the object? You can't delete it then since the
new reference would be dangling. But not deleting the object means
that the __del__ method may be called again later when the reference
count goes down to zero once again. Would this be a problem?

> I don't really have a good example of why you'd want this, other than
> for creating classes that keep track of how many instances of the
> class exist. Perhaps someone else can think of a good reason for
> having it. It just seems that if you have a function that is called at
> object creation that you should have one that is called at object
> destruction as well.

Actually, there are lots of situations where a class is used as a
wrapper around some "real-world" object (e.g. a window or a temporary
file or an audio device) that you would want to destroy (or restore to
a previous state) when the instance goes away. So yes, I think there
are many cases where this would be useful.

There's one problem with __del__, however: what if it raises an
exception? __del__ will be called implicitly from a DECREF(x)
statement in the C code, and I'm not going to add error checking to
all DECREF() statements. So these exceptions will have to be ignored.
In fact, there may already be an exception pending when DECREF() is
called, so it may have to save and restore the original exception.
Nasty!

One final thing to ponder: if we have a __del__ method, should the
interpreter guarantee that it is called when the program exits? (Like
C++, which guarantees that destructors of global variables are
called.) The only way to guarantee this is to go running around all
modules and delete all their variables. But this means that __del__
method cannot trust that any global variables it might want to use
still exist, since there is no way to know in what order variables are
to be deleted. Or is this not a useful feature?

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>

 

Python Gotchas 1: __del__ is not the opposite of __init__ | Algorithm.co.il http://www.algorithm.co.il/blogs/programming/python-gotchas-1-__del__-is-not-the-opposite-of-__init__/

 

class A:
def __init__(self, x):
if x == 0:
raise Exception()
self.x = x

def __del__(self):
print('__del__')
print(self.x)


A(1)
A(0)

 

 

l = [223, 42342, 242423423, 5, 555, 555, 55, 8, 87, 8, 5]
n = len(l)
for i in range(0, n, 1):
    del l[0]
    print(l) 

 

posted @ 2017-10-18 20:05  papering  阅读(113)  评论(0编辑  收藏  举报