python方法和函數的區別

 

 

from types import MethodType, FunctionType


class Foo(object):
    def __init__(self):
        self.name = "MyName"

    def func(self):
        print(self.name)


obj = Foo()
print(isinstance(obj.func, FunctionType))  # False
print(isinstance(obj.func, MethodType))  # True   #說明這是一個方法

print(isinstance(Foo.func, FunctionType))  # True   #說明這是一個函數。
print(isinstance(Foo.func, MethodType))  # False

print(type(obj.func)) # <class 'method'>
print(type(Foo.func)) # <class 'function'>

 

 

REF

https://blog.csdn.net/qq_43422918/article/details/89645735

https://www.zhihu.com/question/317390856/answer/632194800

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章