<jupyter_start><jupyter_text>python functions and methodsFunction:  1. Function is block of code that is also called by its name. (independent)  2. The function can have different parameters or may not have any at all. If any data  (parameters) are passed, they are passed explicitly.  3. It may or may not return any data.  4. Function does not deal with Class and its instance concept.  Method:  1. Method is called by its name, but it is associated to an object (dependent).  2. A method is implicitly passed the object on which it is invoked.  3. It may or may not return any data.  4. A method can operate on the data (instance variables) that is contained by the corresponding class  Difference between method and function:  1. Simply, function and method both look similar as they perform in almost similar way, but the key difference is the concept of ‘Class and its Object‘.  2. Functions can be called only by its name, as it is defined independently. But methods can’t be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.<jupyter_code>import types
class D:
    var = None
    def __init__(self, name):
        self.name = name
        self.msg = None
    def f(self):
        print("f:" + self.name)
def e(self):
    print(self.name)<jupyter_output><empty_output><jupyter_text>D.f 是一个 function:<jupyter_code>print(D.f.__class__)
D.f<jupyter_output><class 'function'><jupyter_text>e 是一个 function:<jupyter_code>print(type(e))
e<jupyter_output><class 'function'><jupyter_text>d.f 是一个 method:<jupyter_code>d = D("hello")
print(d.f.__class__)
d.f()
vars(D)<jupyter_output><empty_output><jupyter_text>将function赋给D的属性，等同于在类里面定义function:<jupyter_code>D.e = e
print(D.e)
vars(D)<jupyter_output><function e at 0x000002CB0E373950><jupyter_text>实例d可以调用方法e,此时，会传入self参数：<jupyter_code>d.e()<jupyter_output>hello<jupyter_text>可以看到 e 此时变成了 d 的一个绑定方法，因此调用 d.e 会传入 self<jupyter_code>d.e<jupyter_output><empty_output><jupyter_text>新建一个 function：<jupyter_code>def g(self, msg):
    self.msg = msg
    print(str(self) + self.name + " " + self.msg)<jupyter_output><empty_output><jupyter_text>将普通函数赋给d，不会产生绑定：<jupyter_code>d.g = g
vars(d)<jupyter_output><empty_output><jupyter_text>调用 g 不会传入self:<jupyter_code>try:
    d.g("msg")
except Exception as e:
    print(e)
del d.g<jupyter_output><empty_output><jupyter_text>使用 types.MethodType 将产生一个绑定到对象的method：<jupyter_code>types.MethodType(g, d)<jupyter_output><empty_output><jupyter_text>该绑定方法可以调用，调用时会自动传入绑定的对象当作第一个参数，此处是 d 传给 self：<jupyter_code>types.MethodType(g, d)("msg")<jupyter_output><__main__.D object at 0x000002CB0E37A7B8>hello msg<jupyter_text>注意，单独调用 types.MethodType，并不会对对象造成影响:<jupyter_code>vars(d)<jupyter_output><empty_output><jupyter_text>所以一般做法是把绑定方法赋值给对象:<jupyter_code>d.g = types.MethodType(g, d)
vars(d)<jupyter_output><empty_output><jupyter_text>此时可以调用该绑定方法：<jupyter_code>d.g("msg-d")<jupyter_output><__main__.D object at 0x000002CB0E37A7B8>hello msg-d<jupyter_text>**注意**  绑定方法会在内部绑定对象，该方法调用时会使用该绑定的对象作为第一个参数（self 或者 cls）  下面可以看到，d.g 和 d1.g 绑定的是同一个对象 d。<jupyter_code>d1 = D("hello")
d1.g = types.MethodType(g, d)
d1.g("msg-d1")
print(d.msg)<jupyter_output><empty_output><jupyter_text>也可以将绑定方法赋值给类：<jupyter_code>def h(cls, data):
    cls.var = data
    print(str(cls) + str(cls.var))

D.h = types.MethodType(h, D)

vars(D)<jupyter_output><empty_output><jupyter_text>D.h 已经将方法绑定到 D，无论是 d.h 还是 D.h 调用，绑定方法都会将已绑定的 D 传给方法的第一个参数，此处是 cls:<jupyter_code>d.h(2)
D.h(3)
print(D.var)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11423012784880489, 'fraction_numerical': 0.015286270150083379, 'mean_word_length': 6.2269076305220885, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '144864', 'license': 'MIT', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1589, 'n_words': 324, 'path': 'types/methodtype.ipynb', 'repo_name': 'TheBigFish/simple-python', 'size': '3598'}