Python extension methods

后端 未结 7 1932
星月不相逢
星月不相逢 2021-02-02 06:39

OK, in C# we have something like:

public static string Destroy(this string s) { 
    return \"\";
}

So basically, when you have a string you ca

相关标签:
7条回答
  • 2021-02-02 07:31

    C# implemented extension methods because it lacks first class functions, Python has them and it is the preferred method for "wrapping" common functionality across disparate classes in Python.

    There are good reasons to believe Python will never have extension methods, simply look at the available built-ins:

    len(o) calls o.__len__
    iter(o) calls o.__iter__
    next(o) calls o.next
    format(o, s) calls o.__format__(s)
    

    Basically, Python likes functions.

    0 讨论(0)
提交回复
热议问题