简体   繁体   English

python 内置类型的扩展方法

[英]Extension method for python built-in types

is it possible to add extension method to python built-in types?是否可以将扩展方法添加到 python 内置类型? I know that I can add extension method to defined type by simply adding new method by.我知道我可以通过简单地添加新方法来将扩展方法添加到定义的类型。 as following:如下:

class myClass:
    pass

myClass.myExtensionMethod = lambda self,x:x * 2
z = myClass()
print z.myExtensionMethod(10)

But is any way to adding extension method to python built'in types like list, dict, ...但是有什么方法可以将扩展方法添加到 python 内置类型,如列表、字典、...

list.myExtension = lambda self,x:x * 2
list.myExtension(10)

It can be done in pure Python with this incredibly clever module:它可以用这个非常聪明的模块在纯 Python 中完成:

https://pypi.python.org/pypi/forbiddenfruit https://pypi.python.org/pypi/forbiddenfruit

For example:例如:

import functools
import ctypes
import __builtin__
import operator

class PyObject(ctypes.Structure):
    pass

Py_ssize_t = hasattr(ctypes.pythonapi, 'Py_InitModule4_64') and ctypes.c_int64 or ctypes.c_int

PyObject._fields_ = [
    ('ob_refcnt', Py_ssize_t),
    ('ob_type', ctypes.POINTER(PyObject)),
]

class SlotsPointer(PyObject):
    _fields_ = [('dict', ctypes.POINTER(PyObject))]

def proxy_builtin(klass):
    name = klass.__name__
    slots = getattr(klass, '__dict__', name)

    pointer = SlotsPointer.from_address(id(slots))
    namespace = {}

    ctypes.pythonapi.PyDict_SetItem(
        ctypes.py_object(namespace),
        ctypes.py_object(name),
        pointer.dict,
    )

    return namespace[name]

def die(message, cls=Exception):
    """
        Raise an exception, allows you to use logical shortcut operators to test for object existence succinctly.

        User.by_name('username') or die('Failed to find user')
    """
    raise cls(message)

def unguido(self, key):
    """
        Attempt to find methods which should really exist on the object instance.
    """
    return functools.partial((getattr(__builtin__, key, None) if hasattr(__builtin__, key) else getattr(operator, key, None)) or die(key, KeyError), self)

class mapper(object):
    def __init__(self, iterator, key):
        self.iterator = iterator
        self.key = key
        self.fn = lambda o: getattr(o, key)

    def __getattribute__(self, key):
        if key in ('iterator', 'fn', 'key'): return object.__getattribute__(self, key)
        return mapper(self, key)

    def __call__(self, *args, **kwargs):
        self.fn = lambda o: (getattr(o, self.key, None) or unguido(o, self.key))(*args, **kwargs)
        return self

    def __iter__(self):
        for value in self.iterator:
            yield self.fn(value)

class foreach(object):
    """
        Creates an output iterator which will apply any functions called on it to every element
        in the input iterator. A kind of chainable version of filter().

        E.g:

        foreach([1, 2, 3]).__add__(2).__str__().replace('3', 'a').upper()

        is equivalent to:

        (str(o + 2).replace('3', 'a').upper() for o in iterator)

        Obviously this is not 'Pythonic'.
    """
    def __init__(self, iterator):
        self.iterator = iterator

    def __getattribute__(self, key):
        if key in ('iterator',): return object.__getattribute__(self, key)
        return mapper(self.iterator, key)

    def __iter__(self):
        for value in self.iterator:
            yield value

proxy_builtin(list)['foreach'] = property(foreach)

import string

print string.join([1, 2, 3].foreach.add(2).str().add(' cookies').upper(), ', ')

>>> 3 COOKIES, 4 COOKIES, 5 COOKIES

There, doesn't that feel good?在那里,感觉不是很好吗?

No. Types defined in C cannot be monkeypatched.不可以。 C 中定义的类型不能进行猴子补丁。

Nope, you gotta subclass!不,你必须子类!

>>> import string
>>> class MyString(str):
...     def disemvowel(self):
...         return MyString(string.translate(self, None, "aeiou"))
... 
>>> s = MyString("this is only a test")
>>> s.disemvowel()
'ths s nly  tst'

Or more specific to your example或更具体到您的示例

>>> class MyList(list):
...     pass
... 
>>> MyList.myExtension = lambda self,x:x * 2
>>> l = MyList()
>>> l.myExtension(10)
20

No, because I'm pretty sure all the built-in types are written in optimized C and thus can't be modified with Python.不,因为我很确定所有内置类型都是用优化的 C 编写的,因此不能用 Python 进行修改。 When I try it, I just get:当我尝试它时,我得到:

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

The best you can do appears to be deriving a class from the built-in type.你能做的最好的似乎是从内置类型派生一个 class 。 For example:例如:

class mylist(list):
    def myfunc(self, x):
        self.append(x)

test = mylist([1,2,3,4])
test.myfunc(99)

(You could even name it "list" so as to get the same constructor, if you wanted.) However, you cannot directly modify a built-in type like the example in your question. (如果需要,您甚至可以将其命名为“list”以获得相同的构造函数。)但是,您不能像问题中的示例那样直接修改内置类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM