Unk*_*ech 29 python string monkeypatching
如何在python中扩展内置类?我想在str类中添加一个方法.
我已经做了一些搜索,但我发现的都是较旧的帖子,我希望有人知道更新的东西.
S.L*_*ott 28
只是对类型进行子类化
>>> class X(str):
... def my_method(self):
... return int(self)
...
>>> s = X("Hi Mom")
>>> s.lower()
'hi mom'
>>> s.my_method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in my_method
ValueError: invalid literal for int() with base 10: 'Hi Mom'
>>> z = X("271828")
>>> z.lower()
'271828'
>>> z.my_method()
271828
Run Code Online (Sandbox Code Playgroud)
小智 11
一种方法是使用"类重新打开"概念(本机存在于Ruby中),可以使用类装饰器在Python中实现.本页提供了一个例子:http: //www.ianbicking.org/blog/2007/08/opening-python-classes.html
我引用:
我认为使用类装饰器你可以这样做:
@extend(SomeClassThatAlreadyExists)
class SomeClassThatAlreadyExists:
def some_method(self, blahblahblah):
stuff
Run Code Online (Sandbox Code Playgroud)
像这样实现:
def extend(class_to_extend):
def decorator(extending_class):
class_to_extend.__dict__.update(extending_class.__dict__)
return class_to_extend
return decorator
Run Code Online (Sandbox Code Playgroud)
假设您不能更改内置类。要在 Python3 中模拟像 Ruby 这样的“类重新打开”,其中__dict__是一个 mappingproxy 对象而不是 dict 对象:
def open(cls):
def update(extension):
for k,v in extension.__dict__.items():
if k != '__dict__':
setattr(cls,k,v)
return cls
return update
class A(object):
def hello(self):
print('Hello!')
A().hello() #=> Hello!
#reopen class A
@open(A)
class A(object):
def hello(self):
print('New hello!')
def bye(self):
print('Bye bye')
A().hello() #=> New hello!
A().bye() #=> Bye bye
Run Code Online (Sandbox Code Playgroud)
在 Python2 中,我也可以编写一个装饰器函数“open”:
def open(cls):
def update(extension):
namespace = dict(cls.__dict__)
namespace.update(dict(extension.__dict__))
return type(cls.__name__,cls.__bases__,namespace)
return update
Run Code Online (Sandbox Code Playgroud)