Python模块\uuuu内置\uuuuu没有属性NoneType

Python模块\uuuu内置\uuuuu没有属性NoneType,python,Python,出于本问题范围之外的原因,我正在构建一个序列化机制。我遇到了一个问题,没有我认为我需要特殊情况的对象。有人能解释为什么NoneType与其他内置类型的处理方式不同吗?还是我遗漏了什么 >>> import sys >>> builtin = sys.modules['__builtin__'] >>> getattr(builtin,'int') <type 'int'> >>> getattr(builtin,

出于本问题范围之外的原因,我正在构建一个序列化机制。我遇到了一个问题,没有我认为我需要特殊情况的对象。有人能解释为什么NoneType与其他内置类型的处理方式不同吗?还是我遗漏了什么

>>> import sys
>>> builtin = sys.modules['__builtin__']
>>> getattr(builtin,'int')
<type 'int'>
>>> getattr(builtin,'list')
<type 'list'>
>>> getattr(builtin,'NoneType')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'NoneType'
工作并返回一个None对象,该对象是NoneType的实例。但是类对象存在于_内置__________________模块中,用于其他所有内容。因此,此代码适用于所有内置类型,但不适用于任何类型:

考虑到这一点,这是有道理的

>>> type(0).__module__
'__builtin__'
>>> type("foo").__module__
'__builtin__'
>>> type(None).__module__
'__builtin__'

这一点在Python中似乎是一个疏忽,除非我遗漏了一些东西。

您是否尝试过查看:

>>> dir(builtin)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

NoneType未作为内置类型列出,即使没有

您是否尝试查看过:

>>> dir(builtin)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
NoneType没有作为内置类型列出,即使None是

您应该尝试:getattrbuiltin,None不是getattrbuiltin,NoneType,因为NoneType是typeNone的值,所以它不是内置类型的属性

您应该尝试:getattrbuiltin,None不是getattrbuiltin,NoneType,因为NoneType是typeNone的值,所以它不是builtin的属性

我想你称之为NoneType的东西是经过特殊处理的,因为你几乎不需要访问它。你可以说

>>> NoneType = type(None)
要将此类型绑定到一个名称,但这并没有共同的用途:isinstancex,NoneType将是x is None的人为等价物;构造函数引发一个TypeError,声明不应该构造实例;从中衍生出来也是被禁止的

如果出于某种高级目的需要,您仍然可以通过typeNone、None或types.NoneType访问该类型。

我想您所谓的NoneType是经过特殊处理的,因为您几乎不需要访问它。你可以说

>>> NoneType = type(None)
要将此类型绑定到一个名称,但这并没有共同的用途:isinstancex,NoneType将是x is None的人为等价物;构造函数引发一个TypeError,声明不应该构造实例;从中衍生出来也是被禁止的


如果出于某些高级目的需要,您仍然可以通过typeNone、None.\uuuu class\uuuuuu或types.NoneType访问该类型。

NoneType在“类型”模块中提供

>>> import types
>>> types.NoneType
<type 'NoneType'>
如果您正在对类型进行任何操作,那么Infact types模块是正确的使用位置,不要依赖内置的类型


这可能是因为没有人不是真正的一种类型,而是一种类型。甚至文档都说None的类型是types.NoneType,但您是对的,它是以一种特殊的方式实现的,与所有其他类型不一致

NoneType在类型模块中可用

>>> import types
>>> types.NoneType
<type 'NoneType'>
如果您正在对类型进行任何操作,那么Infact types模块是正确的使用位置,不要依赖内置的类型


这可能是因为没有人不是真正的一种类型,而是一种类型。甚至文档都说None的类型是types.NoneType,但您是对的,它是以一种特殊的方式实现的,与所有其他类型不一致

这很奇怪。但是python 2中的类和对象也有很多奇怪的地方。对于从简单脚本发展到成熟OO的一系列语言来说,这是事实。Python3旨在解决这个问题

NoneType已经是一个特例,因为它是单例的类对象,所以不能作为构造函数调用

TypeError: cannot create 'NoneType' instances

因此,如果您正在编写一个通用序列化程序,那么您无论如何都需要使用“无特例”,不管您是否无法查找类对象。

这很奇怪。但是python 2中的类和对象也有很多奇怪的地方。对于从简单脚本发展到成熟OO的一系列语言来说,这是事实。Python3旨在解决这个问题

NoneType已经是一个特例,因为它是单例的类对象,所以不能作为构造函数调用

TypeError: cannot create 'NoneType' instances

因此,如果您正在编写一个通用序列化程序,那么您无论如何都需要使用特殊情况None,而不管您是否无法查找类对象。

getattrbuiltin,None。。。也许…getattrbuiltin,没有。。。也许…很明显我不能依赖它,因为它不在那里。但考虑到其他一切都存在,它的省略是没有意义的。@Leopd:当然有意义;为什么要有一个内置的构造函数来生成一个已经内置的单例呢?显然我不能依赖它,因为它不在那里。但考虑到其他一切都存在,它的省略是没有意义的。@Leopd:当然有意义;为什么要有一个内置的构造函数来生成一个已经内置的单例呢?