python 类方法 函数_python 类函数绑定方法的实现

webchat.jpg

实现一个函数描述器This means that all functions are non-data descriptors which return bound methods when they are invoked from an object.

所有的函数都是一个无数据的描述器。类实例调用函数即触发描述器语法,该描述器在类实例被调用时,返回一个绑定的普通方法。

下面实现了一个纯 python 的描述器 BindFunction, 用来绑定方法 f_normal 到函数 f。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36class :

def f(self, name):

print(self, name)

d=D()

d.f("hello")

D.f(d, "hello")

import types

from functools import wraps

def f_normal(self, name):

print(self, name)

class BindFunction(object):

def __init__(self, func):

wraps(func)(self)

def __call__(self, *args, **kwargs):

return self.__wrapped__(*args, **kwargs)

def __get__(self, obj, objtype=None):

"Simulate func_descr_get() in Objects/funcobject.c"

if obj is None:

return self

return types.MethodType(self, obj)

class :

f = BindFunction(f_normal)

d = D()

d.f("world")

D.f(d, "world")

类 BindFunction 也可以实现如下:

1

2

3

4

5

6

7

8

9class BindFunction(object):

def __init__(self, func):

self.func = func

def __get__(self, obj, objtype=None):

"Simulate func_descr_get() in Objects/funcobject.c"

if obj is None:

return self.func

return types.MethodType(self.func, obj)

Functions and MethodsPython’s object oriented features are built upon a function based environment. Using non-data descriptors, the two are merged seamlessly.

Class dictionaries store methods as functions. In a class definition, methods are written using def or lambda, the usual tools for creating functions. Methods only differ from regular functions in that the first argument is reserved for the object instance. By Python convention, the instance reference is called self but may be called this or any other variable name.

To support method calls, functions include the __get__() method for binding methods during attribute access. This means that all functions are non-data descriptors which return bound methods when they are invoked from an object. In pure Python, it works like this:

class Function(object):

. . .

def __get__(self, obj, objtype=None):

"Simulate func_descr_get() in Objects/funcobject.c"

if obj is None:

return self

return types.MethodType(self, obj)

Running the interpreter shows how the function descriptor works in practice:

>>>

>>> class D(object):

... def f(self, x):

... return x

...

>>> d = D()

# Access through the class dictionary does not invoke __get__.

# It just returns the underlying function object.

>>> D.__dict__['f']

# Dotted access from a class calls __get__() which just returns

# the underlying function unchanged.

>>> D.f

# The function has a __qualname__ attribute to support introspection

>>> D.f.__qualname__

'D.f'

# Dotted access from an instance calls __get__() which returns the

# function wrapped in a bound method object

>>> d.f

>

# Internally, the bound method stores the underlying function,

# the bound instance, and the class of the bound instance.

>>> d.f.__func__

>>> d.f.__self__

<__main__.D object at 0x1012e1f98>

>>> d.f.__class__

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值