重新定义 Python 内置数据类型
是否可以重新定义括号 [] 使用哪个对象?
我可以对 list
对象进行子类化,但是如何让解释器使用我的子类来代替内置列表对象?是否可以?
(我很确定我在这个问题上使用了错误的术语 - 请随意编辑)
>>> 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
>>>
我对此没有实际用途 - 只是好奇。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
括号是语言的一部分。它们用于创建列表。不可能重新定义它(也不可取!)。
The brackets are part of the language. They're used to create lists. It's not possible to redefine that (and not desirable either!).
运行您发布的代码后尝试运行代码
现在,使用我发布的代码确定类型
似乎
[]
创建list
,而不是 < code>mlist,看起来很奇怪:S更新
我检查了使用
dis
生成的字节码,生成了下面的代码看起来
list< /code> 将调用分配给它的任何内容,而
[]
将转换为BUILD_LIST
字节码。看来[]
没有转换为list
,因此[]
的行为停留在创建列表上。更新2
Python类可以更新
嗯,除了内置类,比如list
Try running the code after you've run the code you posted
Now, determine the type using the code I've posted
it seems that
[]
createslist
, instead ofmlist
, it looks strange :SUpdate
I checked the bytecode generated using
dis
, and the code below was generatedIt appears that
list
will invoke whatever is assigned to it, while[]
will be converted toBUILD_LIST
bytecode. It appears that[]
is not translated tolist
, hence[]
's behavior is stucked to creating list.Update 2
Python class can be updated
Well, except for builtin classes, like list
您可以使用 not 来替换 list 为 mlist ,正如您所尝试的那样,
而是简单地
使用(您在解释器中运行它可能会遇到问题,因为 mlist() 递归地调用 list() 等。但是如果您将定义 mlist 的代码放在一个不同的范围,例如在您导入的模块中,那么它将起作用。)然后您可以创建一个新的 mlist,
但有趣的是,
我认为这在语法上不是等价的。显然 [ ] 被硬编码为调用内置列表类型,而不是当前命名为“列表”的任何对象。
You can replace list with mlist by using not, as you tried,
but simply
(You might have problems running this in the interpreter, because mlist() calls list() and so on recursively. But if you put the code that defines mlist in a different scope, such as in a module you import, then it will work.) Then you can create a new mlist by
but interestingly, not by
which I had thought was syntactically equivalent. Apparently [ ] is hardcoded to call the built-in list type, rather than whatever object is currently named "list".
这是有可能的。如果你愿意付出足够多的努力,大多数事情在软件中都是可能的。 :) 当然,这是一个坏主意。如果您使用此类更改编写任何软件,则会出现许多问题:
这是一个 CPython 扩展模块,来自2003(和示例)适用于 Python 2.3。它很可能需要更新才能与更新版本的 Python 一起使用,但它演示了这种特定方法需要多么肮脏。
另一种方法是在语法层面进行改变。 Logix 提供了用于此方法的工具。这涉及较少的 C 黑客攻击,但带来了全新的解析器和编译器。
It's possible. Most things are possible in software, if you're willing to get sufficiently dirty. :) It's a bad idea, of course. If you wrote any software using such a change, it would have a number of problems:
Here's a CPython extension module from 2003 (and example) which works with Python 2.3. It very likely needs to be updated to work with more recent versions of Python, but it demonstrates how dirty you need to get for this particular approach.
Another approach is to change things at the level of the grammar. Logix provides tools for this approach. This involves less C hacking, but brings in an entire new parser and compiler.