function和bound method的区别

Python官方文档

Method被调用时,self会自动加到函数列表首位;

Method就是封装了一个func和一个class object;

Function的__get__可以把自己和一个class object封装到一个bounded method里面去;

 

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

Functions stored in class dictionaries get turned into methods when invoked. Methods only differ from regular functions in that the object instance is prepended to the other arguments. By convention, the instance is called self but could be called this or any other variable name.

Methods can be created manually with types.MethodType which is roughly equivalent to:

class MethodType:
    "Emulate Py_MethodType in Objects/classobject.c"

    def __init__(self, func, obj):
        self.__func__ = func
        self.__self__ = obj

    def __call__(self, *args, **kwargs):
        func = self.__func__
        obj = self.__self__
        return func(obj, *args, **kwargs)

To support automatic creation of methods, functions include the __get__() method for binding methods during attribute access. This means that functions are non-data descriptors that return bound methods during dotted lookup from an instance. Here’s how it works:

class Function:
    ...

    def __get__(self, obj, objtype=None):
        "Simulate func_descr_get() in Objects/funcobject.c"
        if obj is None:
            return self
        return MethodType(self, obj)

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

class D:
    def f(self, x):
         return x

The function has a qualified name attribute to support introspection:

>>>

>>> D.f.__qualname__
'D.f'

Accessing the function through the class dictionary does not invoke __get__(). Instead, it just returns the underlying function object:

>>>

>>> D.__dict__['f']
<function D.f at 0x00C45070>

Dotted access from a class calls __get__() which just returns the underlying function unchanged:

>>>

>>> D.f
<function D.f at 0x00C45070>

The interesting behavior occurs during dotted access from an instance. The dotted lookup calls __get__() which returns a bound method object:

>>>

>>> d = D()
>>> d.f
<bound method D.f of <__main__.D object at 0x00B18C90>>

Internally, the bound method stores the underlying function and the bound instance:

>>>

>>> d.f.__func__
<function D.f at 0x1012e5ae8>

>>> d.f.__self__
<__main__.D object at 0x1012e1f98>

If you have ever wondered where self comes from in regular methods or where cls comes from in class methods, this is it!

### C++ 中 `std::function` 的用法 `std::function` 是 C++ 标准库中的一个类模板,用于封装可调用对象(如函数指针、lambda 表达式、绑定表达式或其他可调用的对象)。这使得可以将任何类型的回调机制统一处理。 #### 基本定义与声明 为了使用 `std::function`,需要包含头文件 `<functional>`。下面是一个简单的例子来展示如何声明初始化它: ```cpp #include <iostream> #include <functional> // 定义一个普通的函数作为示例 void greet(const std::string& name) { std::cout << "Hello, " << name << "!" << std::endl; } int main() { // 使用 std::function 来存储指向 void(std::string const&) 类型的函数 std::function<void(const std::string&)> func = greet; // 调用该函数 func("World"); return 0; } ``` 这段代码展示了怎样创建并赋值给 `std::function` 对象[^1]。 #### 存储 Lambda 表达式 除了普通函数外,还可以把 lambda 表达式存入到 `std::function` 中去: ```cpp #include <iostream> #include <functional> int main(){ // 创建一个带有捕获列表的 lambda 函数 int factor = 2; auto multiplyByFactor = [factor](int num){return num * factor;}; // 将其转换成 std::function std::function<int(int)> fnMultiply = multiplyByFactor; // 执行这个函数 std::cout << "Result is: " << fnMultiply(5) << '\n'; return 0; } ``` 这里说明了如何通过捕捉外部变量的方式构建更复杂的逻辑,并将其保存至 `std::function` 实例中。 #### 绑定成员函数 对于类的方法也可以利用 `std::bind()` 或者直接传递实例加方法名的方式来实现同样的效果: ```cpp #include <iostream> #include <memory> #include <functional> class MyClass { public: void say_hello(const std::string& message){ std::cout << "MyClass says:" << message << "\n"; } }; int main(){ auto obj_ptr = std::make_shared<MyClass>(); // 方法一:使用 bind() std::function<void(const std::string&)> bound_method_1 = std::bind(&MyClass::say_hello, obj_ptr, std::placeholders::_1); // 方法二:直接指定对象及其成员函数 std::function<void(const std::string&)> bound_method_2 = [&obj_ptr](const std::string& msg){ obj_ptr->say_hello(msg); }; // 测试两种方式的效果相同 bound_method_1("hello from method 1."); bound_method_2("hello from method 2."); return 0; } ``` 上述片段解释了当涉及到面向对象编程时,应该如何操作类内的非静态成员函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值