python计算空类型_python-为什么SimpleNamespace的大小与空类的大...

考虑以下:

In [1]: import types

In [2]: class A:

...: pass

...:

In [3]: a1 = A()

In [4]: a1.a, a1.b, a1.c = 1, 2, 3

In [5]: a2 = types.SimpleNamespace(a=1,b=2,c=3)

In [6]: sys.getsizeof(a1)

Out[6]: 56

In [7]: sys.getsizeof(a2)

Out[7]: 48

尺寸差异来自何处?看着:

In [10]: types.__file__

Out[10]: '/Users/juan/anaconda3/lib/python3.5/types.py'

我发现:

import sys

# Iterators in Python aren't a matter of type but of protocol. A large

# and changing number of builtin types implement *some* flavor of

# iterator. Don't check the type! Use hasattr to check for both

# "__iter__" and "__next__" attributes instead.

def _f(): pass

FunctionType = type(_f)

LambdaType = type(lambda: None) # Same as FunctionType

CodeType = type(_f.__code__)

MappingProxyType = type(type.__dict__)

SimpleNamespace = type(sys.implementation)

好吧,这什么都没做:

>>> import sys

>>> sys.implementation

namespace(cache_tag='cpython-35', hexversion=50660080, name='cpython', version=sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0))

>>> type(sys.implementation)

我似乎在这里追逐自己的尾巴.

我在64位系统上使用CPython 3.5.对于一些我无法查明的错误引用,这8个字节似乎正好合适.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值