首页 > python 中 method_descriptor 如何理解?

python 中 method_descriptor 如何理解?

type(str.center)

python 中 method_descriptor 如何理解?


str.center('a', 3)'a'.center(3)作用是一样的

stackoverflow上的这个回答非常棒:

http://stackoverflow.com/questions/1180303/static-vs-instance-methods-of-str-in-python

原理:

class C:
    def method(self, arg):
        print "In C.method, with", arg

o = C()
o.method(1)
C.method(o, 1)
# Prints:
# In C.method, with 1
# In C.method, with 1

o.method(1) 可以看作是 C.method(o, 1)的简写。

【热门文章】
【热门文章】