[英]How to add method to base class?
I want to add __add__
and __radd__
to the python base class set
.我想将
__add__
和__radd__
添加到 python base class set
中。
The code can be as simple as代码可以很简单
def __add__(self, other) :
assert isinstance(other, set), \
"perhaps additional type checking or argument validation can go here but" + \
" strictly only defined for pure python sets"
return set( list(self) + list(other) )
def __radd__(self, other) :
assert isinstance(other, set), \
"perhaps additional type checking or argument validation can go here but" + \
" strictly only defined for pure sets"
return set( list(other) + list(self) )
What is the pythonic implementation of this and how can I extend the base class without creating my own MySet
class that takes set
as a parent class?这个的 pythonic 实现是什么?我如何在不创建自己的
MySet
class 的情况下扩展基数 class,将set
作为父 class? Can I just use set.__add__ = some_function_I_defined
?我可以只使用
set.__add__ = some_function_I_defined
吗?
What you should do imho is subclass the built-in set
class. In Python (contrary to eg Ruby or JavaScript) monkey-patching a built-in is not allowed.恕我直言,你应该做的是将内置
set
class 子类化。在 Python 中(与 Ruby 或 JavaScript 相反)猴子修补内置是不允许的。
So eg trying to add a non-existent method:因此,例如尝试添加一个不存在的方法:
x = [1,2,3]
x.my_new_method_added_in_runtime = lambda: "whatever"
is not going to work, you'd get AttributeError: 'list' object has no attribute 'my_new_method_added_in_runtime'
不会工作,你会得到
AttributeError: 'list' object has no attribute 'my_new_method_added_in_runtime'
You cannot also modify the existing methods of objects instantiated using those built-ins:您也不能修改使用这些内置函数实例化的对象的现有方法:
x = [1,2,3]
x.sort = lambda: "returning some string instead of sorting..."
will result in AttributeError: 'list' object attribute 'sort' is read-only
将导致
AttributeError: 'list' object attribute 'sort' is read-only
And和
list.append = None
# OR
del list.append
will result in: TypeError: can't set attributes of built-in/extension type 'list'
将导致:
TypeError: can't set attributes of built-in/extension type 'list'
All of the above is true for set
as well an so on.以上所有内容都适用于
set
以及依此类推。
You could try to look for some libraries to achieve that eg https://pypi.org/project/forbiddenfruit/0.1.0/ , but it's strongly discouraged.您可以尝试寻找一些库来实现该目标,例如https://pypi.org/project/forbiddenfruit/0.1.0/ ,但强烈建议不要这样做。
@chepner correctly answered my question without realizing it. @chepner 在没有意识到的情况下正确地回答了我的问题。 I was trying to reimplement already existing functionality since I was not familiar with python sets.
因为我不熟悉 python 集,所以我试图重新实现现有的功能。
The act of joining two sets, 'taking their union' is implemented with the python __or__
and __ior__
methods.加入两个集合的行为,“合并”是通过 python
__or__
和__ior__
方法实现的。 The operation I needed specifically was |
我特别需要的操作是
|
and |=
.和
|=
。
In principle we should be able to set operations as chepner suggested with set.__add__ = set.__or__
, but as chepner points out this results in an error:原则上,我们应该能够按照 chepner 的建议设置操作
set.__add__ = set.__or__
,但正如 chepner 指出的那样,这会导致错误:
TypeError: cannot set '__add__' attribute of immutable type 'set'
Thank you all.谢谢你们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.