class.__name__

My (very) recent patch #635933 allows assignment to both __name__ and
__bases__ of new-style classes. Given that the code for __bases__ is
much more complicated, it's a little odd that __name__ is the one
still giving me headaches.

It's all to do with dots.

An extension type like (e.g.) time.struct_time is created with a
tp_name of 'time.struct_time' which the accessors for __module__ and
__name__ translate thusly:

>>> time.struct_time.__name__
'struct_time'
>>> time.struct_time.__module__
'time'

User defined new-style classes _seem_ to behave similary:

>>> class C(object):
... pass
...
>>> C.__name__
'C'
>>> C.__module__
'__main__'

but under the hood it's quite different: tp_name is just "C" and
'__module__' is a key in C.__dict__. This shows up when in:

>>> C.__name__ = 'C.D'
>>> C.__name__
'D'
>>> C.__module__
'C'

 

 

 

 

转载于:https://my.oschina.net/YcOk14itqTKB/blog/4242

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值