且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 2.4删除无数据类型?

更新时间:2023-02-02 21:45:05

[ga ******** @ gmail.com] 我刚刚阅读了Python 2.4中的新内容文档,将无
数据类型转换为常量:
http://python.org/doc/2.4/whatsnew/node15.html

"""
#None现在是一个常量;将新值绑定到名称的代码
无现在是语法错误。
"""

那么,这有什么影响?


对于任何合理的代码都没有任何影响。你不能再做这样的事了:
None = 2
SyntaxError:赋值给None


这就是全部。

我发现缺乏解释有点令人费解,因为我编写了代码来比较变量''无'类型的类型。例如,变量
将被初始化为None,如果它经历了一个不变的循环,我可以通过使用条件类型(var)== type来确定这一点(没有)。


Python'的None是一个单例,意味着有一个类型类型(None)的单一,唯一的
实例。因此,在您描述的情况下,如果var为None,则测试


是正确的,并且是惯用的:
>
而不是。检查类型(var)对类型(无)仍然有效,但

从来都不是***的方法。


如果你有一个像这样的表达式


var =无


没关系。你没有改变名称的绑定无。那里,

你正在更改名称var的绑定。

什么会输入(无)现在返回?




没有改变:

类型(无)



< type' 'NoneType''>


在文章< 11 ********************** @ f14g2000cwb .googlegroups .com>,
ga********@gmail.com &lt ; GA ******** @ gmail.com&GT;写道:


我刚刚阅读了Python 2.4中的新内容文档,将无
数据类型转换为常量:
http://python.org/doc/2.4 /whatsnew/node15.html

"""
#None现在是一个常数;将新值绑定到名称的代码
无现在是语法错误。
"""

那么,这有什么影响?我发现缺乏解释一点点令人费解,因为我编写的代码将变量的'
类型与''无'类型进行比较。例如,一个变量将被初始化为''None'',如果它经历了一个不变的循环,我可以通过使用条件类型(var)==
型(无)。什么会打字(无)现在返回?




根据经验,NoneType,和以前一样。您所指的更改是

,以防止用户做愚蠢的事情,例如:


============== ==================================== ============== ==========

Python 2.3.4(#1,2005年2月8日,13:07:40)

[GCC 3.3.3( NetBSD nb3 20040520)] on netbsd2

输入help,copyright,credit等等。或许可证或更多信息。

type(None)
< type''NonType''>无=&QUOT; fred的&QUOT;
< stdin>:1:语法警告:分配给无无
''fred''类型(无)
< type''str''>
============================================== ==== ========================

Python 2.4(#1,2005年3月4日,16:55: 16)

[GCC 3.3.3(NetBSD nb3 20040520)]在netbsd2上

输入help,copyright,credit等等。或许可证或欲获得更多信息。 type(None)
< type''NonType''>无=&QUOT;佛瑞德&QUOT;
语法错误:赋值为无类型(无)
< type''NoneType''>



=== =============================================== === =====================


所以你的成语应该仍然有用。请注意,由于只有

一个NoneType实例(即无),您只需测试var is None

并为自己节省一些工作。


Gary Duzan

BBN Technologies


ga ******** @ gmail.com 写道:
我刚刚读了''什么'' Python 2.4中的新文档将None
数据类型转换为常量:
http://python.org/doc/2.4/whatsnew/node15.html




含义:A Python中长期存在的疣现在消失了。它的时间到了
幸灾乐祸。 Python中有任何真正邪恶的故事吗?现在去看看

Perl然后回来说谢谢 - 我选择 - 我正在使用Python。

Warren


I just read in the ''What''s New in Python 2.4'' document that the None
data type was converted to a constant:
http://python.org/doc/2.4/whatsnew/node15.html

"""
# None is now a constant; code that binds a new value to the name
"None" is now a syntax error.
"""

So, what''s the implications of this? I find the lack of explanation a
little puzzling, since I''ve written code that compares a variable''s
type with the ''None'' type. For example, a variable would be
initialized to ''None'' and if it went through a loop unchanged, I could
determine this at the end by using a conditional type(var) ==
type(None). What will type(None) return now?

[ga********@gmail.com]
I just read in the ''What''s New in Python 2.4'' document that the None
data type was converted to a constant:
http://python.org/doc/2.4/whatsnew/node15.html

"""
# None is now a constant; code that binds a new value to the name
"None" is now a syntax error.
"""

So, what''s the implications of this?
No implications, for any sane code. You can no longer do things like this:
None = 2 SyntaxError: assignment to None

That''s all.
I find the lack of explanation a little puzzling, since I''ve written code that
compares a variable''s type with the ''None'' type. For example, a variable
would be initialized to ''None'' and if it went through a loop unchanged, I could
determine this at the end by using a conditional type(var) == type(None).
Python''s None is a singleton, meaning that there is a single, unique
instance of type type(None). So in the case you describe, it''s
correct, and idiomatic, to test

if var is None:

instead. Checking type(var) against type(None) will still work, but
was never the best way to do it.

If you have an expression like

var = None

that''s fine. You''re not changing the binding of name "None" there,
you''re changing the binding of name "var".
What will type(None) return now?



That hasn''t changed:
type(None)


<type ''NoneType''>


In article <11**********************@f14g2000cwb.googlegroups .com>,
ga********@gmail.com <ga********@gmail.com> wrote:


I just read in the ''What''s New in Python 2.4'' document that the None
data type was converted to a constant:
http://python.org/doc/2.4/whatsnew/node15.html

"""
# None is now a constant; code that binds a new value to the name
"None" is now a syntax error.
"""

So, what''s the implications of this? I find the lack of explanation a
little puzzling, since I''ve written code that compares a variable''s
type with the ''None'' type. For example, a variable would be
initialized to ''None'' and if it went through a loop unchanged, I could
determine this at the end by using a conditional type(var) ==
type(None). What will type(None) return now?



Empirically, NoneType, same as before. The change you refer to is
to prevent the user from doing silly things like:

================================================== ========================
Python 2.3.4 (#1, Feb 8 2005, 13:07:40)
[GCC 3.3.3 (NetBSD nb3 20040520)] on netbsd2
Type "help", "copyright", "credits" or "license" for more information.

type(None) <type ''NoneType''> None="fred" <stdin>:1: SyntaxWarning: assignment to None None ''fred'' type(None) <type ''str''> ================================================== ========================
Python 2.4 (#1, Mar 4 2005, 16:55:16)
[GCC 3.3.3 (NetBSD nb3 20040520)] on netbsd2
Type "help", "copyright", "credits" or "license" for more information. type(None) <type ''NoneType''> None="Fred" SyntaxError: assignment to None type(None) <type ''NoneType''>


================================================== ========================

So your idiom should still work. Note that since there is only
one NoneType instance (i.e. None), you can just test for "var is None"
and save yourself some work.

Gary Duzan
BBN Technologies


ga********@gmail.com wrote:
I just read in the ''What''s New in Python 2.4'' document that the None
data type was converted to a constant:
http://python.org/doc/2.4/whatsnew/node15.html



Implication: A long standing wart in Python now gone. Its time to
gloat. Are there any really evil glitches LEFT in Python? Now go look at
Perl and come back and say "Thank-deity-of-my-choice-I''m-using-Python".
Warren