python内置类型,重新定义Python内置数据类型

Is it possible to redefine which object the brackets [] use?

I can subclass the list object, but how to I make the interpreter use my subclass in place of the buildin list object? Is it possible?

(I'm pretty sure I'm using the wrong terms for the question- feel free to edit)

>>> class mlist(list):

... def __init__(self):

... list.__init__(self)

... def __getitem__(self, item):

... return list.__getitem__(self, item) * 2

...

>>> testlist = mlist()

>>> testlist.append(21)

>>> testlist[0]

42

>>> list = mlist() # maybe setting the 'list' type will do it?

>>> testlist = []

>>> testlist.append(21)

>>> testlist[0]

21 # Nope

>>>

I don't have a practical use for this- just curious.

解决方案

Try running the code after you've run the code you posted

>>> testlist = list()

Traceback (most recent call last):

File "", line 1, in

TypeError: 'mlist' object is not callable

Now, determine the type using the code I've posted

>>> type([])

>>> type(list)

>>> type(testlist)

it seems that [] creates list, instead of mlist, it looks strange :S

Update

I checked the bytecode generated using dis, and the code below was generated

>>> import dis # python's disassembler

>>> def code1():

... return []

...

>>> dis.dis(code1)

2 0 BUILD_LIST 0

3 RETURN_VALUE

>>> def code2():

... return list()

...

>>> dis.dis(code2)

2 0 LOAD_GLOBAL 0 (list)

3 CALL_FUNCTION 0

6 RETURN_VALUE

It appears that list will invoke whatever is assigned to it, while [] will be converted to BUILD_LIST bytecode. It appears that [] is not translated to list, hence []'s behavior is stucked to creating list.

Update 2

Python class can be updated

>>> class NewList(list):

... pass

...

>>> a = NewList()

>>> a.append(23)

>>> a[0]

23

>>> def double_getitem(self, key):

... return list.__getitem__(self, key) * 2

...

>>> NewList.__getitem__ = double_getitem

>>> a[0]

46

Well, except for builtin classes, like list

>>> list.__getitem__ = double_getitem

Traceback (most recent call last):

File "", line 1, in

TypeError: can't set attributes of built-in/extension type 'list'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值