Weakrefs到派生自str的类 [英] Weakrefs to classes that derive from str

查看:77
本文介绍了Weakrefs到派生自str的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不起作用?

Why doesn''t this work?

来自weakref import ref
C类( str):传递
.... ref(C())
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:无法创建对''C''对象的弱引用


请注意,这确实有效:

class D(int):传递
.... ref(D())
< weakref at 0x53e10;死>
from weakref import ref
class C(str): pass .... ref(C()) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot create weak reference to ''C'' object
Note that this does work:
class D(int): pass .... ref(D()) <weakref at 0x53e10; dead>




同样适用于浮点数,列表等等。除了strs之外的所有内容。


rg



Likewise for floats, lists, etc. Everything but strs.

rg

推荐答案

Ron Garret写道:
Ron Garret wrote:
为什么这不起作用?
Why doesn''t this work?
来自weakref import ref
class C(str):pass ... ref(C())
from weakref import ref
class C(str): pass ...ref(C())


Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:无法创建对''C''对象的弱引用


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot create weak reference to ''C'' object




请注意,您不需要课程重定向:


py> ref('''')

回溯(最近一次调用最后一次):

文件"<交互式输入>",第1行,在?

TypeError:无法创建对''str''对象的弱引用


但我不知道为什么字符串不是参考的有效参数...


STeVe



Note that you don''t need the class redirection:

py> ref('''')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: cannot create weak reference to ''str'' object

But I don''t know why strings aren''t valid arguments to ref...

STeVe


文章< bv ***************** ***@comcast.com> ;,

Steven Bethard< st ************ @ gmail.com>写道:
In article <bv********************@comcast.com>,
Steven Bethard <st************@gmail.com> wrote:
Ron Garret写道:
Ron Garret wrote:
为什么这不起作用?
Why doesn''t this work?
>来自weakref import ref
> class C(str):pass
>from weakref import ref
>class C(str): pass


...


...

> ref(C())
>ref(C())


回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
TypeError:无法创建对''C''对象的弱引用


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot create weak reference to ''C'' object



请注意,您不需要课程重定向:

py> ref('''')
Traceback(最近一次调用最后一次):
文件"< interactive input>",第1行,在?
TypeError:无法创建弱引用''str''对象

但我不知道为什么字符串不是参考的有效参数......



Note that you don''t need the class redirection:

py> ref('''')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: cannot create weak reference to ''str'' object

But I don''t know why strings aren''t valid arguments to ref...




没有任何本机类型(int,float,list,tuple等)可以有弱的

引用,但是将它们包装在一个类中应该可以解决这个问题。

它确实 - 除了str之外的所有课程。


rg



None of the native types (int, float, list, tuple, etc.) can have weak
references, but wrapping them in a class is supposed to get around that.
And it does -- for all classes except str.

rg


Ron Garret写道:
Ron Garret wrote:
请注意,您不需要课程重定向:

py> ref('''')
Traceback(最近一次调用最后一次):
文件"< interactive input>",第1行,在?
TypeError:无法创建弱引用''str''对象

但我不知道为什么字符串不是参考的有效参数...
Note that you don''t need the class redirection:

py> ref('''')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: cannot create weak reference to ''str'' object

But I don''t know why strings aren''t valid arguments to ref...



没有本机类型(int,float,list,tuple等)可以有弱的引用,但是将它们包装在一个类中应该可以解决这个问题。
它确实 - 除了str之外的所有类。



None of the native types (int, float, list, tuple, etc.) can have weak
references, but wrapping them in a class is supposed to get around that.
And it does -- for all classes except str.




有趣。包装物是否记录在某处?我在weakref.ref的文档中没有看到

(虽然我偶尔会知道

盲);)

STeVe



Interesting. Is the wrapping thing documented somewhere? I didn''t see
it in the documentation for weakref.ref (though I have been known to be
blind occasionally) ;)

STeVe


这篇关于Weakrefs到派生自str的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆