python中<class 'int'>什么意思_#python3type(1)<class,'int'>为什么和python核心编程里的不一样?...

补充一点,type是个对象,type(1)也是个对象

在交互环境下输出type(1)或者print(type(1)) 相当于type.__repr__(int)和type.__str__(int)

下面说的“统一”,我想说的是python2里type的输出会有和两种情况,这应该是由于历史原因吧(旧式类和新式类,下面的引用和链接里也有相关的内容),然后python3里没有了旧式类,就达成了统一

class Foo:

pass

class Bar(object):

pass

# 具体关于type可以看下面的链接的 Python Types and Objects

type.__repr__(int) # ""

type.__repr__(type(Foo)) # "" Foo是旧式类,Foo本身不是type的实例,Foo()是通过内置方法instance创建的

type.__repr__(Bar) # "" Bar是新式类,本身就是type的实例

# type和class可能就是用于区分这两类吧

查了些资料,之前写的其实和原问题没啥关系... 不过我也不确定现在的回答是否完全正确,希望有大神解答

在Python2.2之后的版本中,应该是统一了class和type,然后在Python3里全部换为了class

在python2.2的文档The built-in 'type' is the most common metaclass; it is the metaclass of all built-in types. Classic classes use a different metaclass: the type known as types.ClassType. The latter is relatively uninteresting; it's a historical artefact that's needed to give classic classes their classic behavior. You can't get to the metaclass of a classic instance using x.__class__.__class__; you have to use type(x.__class__), because classic classes don't support the __class__ attribute on classes (only on instances).

下面的链接也可以看一下。

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值