可以重写Python中的文字吗? [英] Can literals in Python be overridden?
问题描述
找不到更好的标题标题的方法,请随时进行纠正.
Couldn't find a way to phrase the title better, feel free to correct.
我是Python的新手,目前正在尝试使用该语言..我注意到,所有内置类型都不能用其他成员扩展..例如,我想向其中添加each
方法list
类型,但这是不可能的.我意识到出于效率方面的考虑而设计这种方式,并且大多数内置类型都是用C实现的.
I'm pretty new to Python, currently experimenting with the language.. I've noticed that all built-ins types cannot be extended with other members.. I'd like for example to add an each
method to the list
type, but that would be impossible. I realize that it's designed that way for efficiency reasons, and that most of the built-ins types are implemented in C.
好吧,我发现要重写此行为的一个原因是定义了一个新类,该类扩展了list
,但除此之外什么也没做.然后,我可以将变量list
分配给该新类,并且每次我想实例化一个新列表时,我都会使用list
构造函数,就像它会被用来创建原始的list
类型一样
Well, one why I found to override this behavior is be defining a new class, which extends list
but otherwise does nothing. Then I can assign the variable list
to that new class, and each time I would like to instantiate a new list, I'd use the list
constructor, like it would have been used to create the original list
type.
class MyList(list):
def each(self, func):
for item in self:
func(item)
list = MyList
my_list = list((1,2,3,4))
my_list.each(lambda x: print(x))
输出:
1
2
3
4
当然,可以通过定义一个获取其内置类型并返回扩展该类型的类的方法来概括该思想.此外,原始的list
变量可以保存在另一个变量中,以保持对其的访问.
The idea can be generalize of course by defining a method that gets a built it type and returns a class that extends that type. Further more, the original list
variable can be saved in another variable to keep an access to it.
我现在面临的唯一问题是,当您以字面形式实例化list
时(即[1,2,3,4]
),它仍将使用原始列表构造函数(或者是?).有没有方法可以覆盖此行为?如果答案是否定的,您是否知道使用户能够扩展内置类型的其他方法? (就像javascript允许扩展内置原型一样.)
Only problem I'm facing right now, is that when you instantiate a list
by its literal form, (i.e. [1,2,3,4]
), it will still use the original list constructor (or does it?). Is there a way to override this behavior? If the answer is no, do you know of some other way of enabling the user to extend the built-ins types? (just like javascript allows extending built-ins prototypes).
我发现内置的这种限制(无法向其添加成员)是Python的缺点之一,使其与其他用户定义的类型不一致...总的来说,我真的很喜欢这种语言,但我确实不喜欢这种语言.了解为什么确实需要此限制.
I find this limitation of built-ins (being unable to add members to them) one of Python's drawbacks, making it inconsistent with other user-defined types... Overall I really love the language, and I really don't understand why this limitation is REALLY necessary.
推荐答案
这是Python的明智选择.
This is a conscientious choice from Python.
首先,关于修补内置类型,这主要是设计决策,其次才是优化.我从潜伏在Python邮件列表中的很多教训中了解到,对内置类型进行猴子修补,尽管对于小型脚本而言很有趣,但在更大的范围中并没有什么用处.
Firstly, with regards to patching inbuilt type, this is primarily a design decision and only secondarily an optimization. I have learnt from much lurking on the Python Mailing List that monkey patching on builtin types, although enjoyable for small scripts, serves no good purpose in anything larger.
一方面,图书馆对类型做出某些假设.如果鼓励扩展默认类型,那么许多库最终将相互争斗.这也会阻止制作新类型-双端队列是双端队列,有序集合是有序集合,字典是字典,应该是这样.
Libraries, for one, make certain assumptions about types. If it were encouraged to extend default types, many libraries would end up fighting each other. It would also discourage making new types – a deque is a deque, an ordered set is an ordered set, a dictionary is a dictionary and that should be that.
文学语法是特别重要的一点.如果您不能保证[1, 2, 3]
是列表,那么可以保证什么?如果人们可以改变这些行为,将会对全球产生巨大影响,从而破坏许多代码的稳定性.不建议使用 goto 和全局变量.
Literal syntax is a particularly important point. If you cannot guarantee that [1, 2, 3]
is a list, what can you guarantee? If people could change those behaviours it would have such global impacts as to destroy the stability of a lot of code. There is a reason goto and global variables are discouraged.
不过,我很喜欢一种特别的技巧.当您看到r"hello"
时,这似乎是扩展的文字形式.
There is one particular hack that I am fond of, though. When you see r"hello"
, this seems to be an extended literal form.
那为什么不r[1, 2, 3]
?
class ListPrefixer:
def __init__(self, typ):
self.typ = typ
def __getitem__(self, args):
return self.typ(args)
class MyList(list):
def each(self, func):
return MyList(func(x) for x in self)
e = ListPrefixer(MyList)
e[1, 2, 3, 4].each(lambda x: x**2)
#>>> [1, 4, 9, 16]
最后,如果您确实想进行更深入的AST黑客攻击,请查看 MacroPy .
这篇关于可以重写Python中的文字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!