Member functions in Python

简介:

self.func is a operation that bind self to the function.

class A(object):
    def __init__(self):
        self.b = B()
        self.b.run(self.func)

    def func(self):
        print "callback"

class B(object):
    def run(self, cb):
        self.cb = cb
        self.cb()

import sys
foo = A()
print sys.getrefcount(foo) # output: 3

Here are 3 objects ref to foo:

  • foo as a local variable,
  • a ref in getrefcount
  • a ref in foo.b.cb

It's more like self.func is func.self = self, bind self to the function.



目录
相关文章
|
12月前
|
Go C# Python
Python Tricks:Python‘s Functions Are First-Class
Python Tricks:Python‘s Functions Are First-Class
91 3
|
12月前
|
Go C# Python
Python Tricks :Lambdas Are Single-Expression Functions 原创
Python Tricks :Lambdas Are Single-Expression Functions 原创
56 1
|
Python
【Azure 应用服务】Python Function App重新部署后,出现 Azure Functions runtime is unreachable 错误
【Azure 应用服务】Python Function App重新部署后,出现 Azure Functions runtime is unreachable 错误
103 2
|
数据处理 Python
Python中的偏函数(Partial Functions)
Python中的偏函数是来自函数式编程的一个强大工具,它的主要目标是减少函数调用的复杂性。这个概念可能起初看起来有点困难理解,但一旦你明白了它的工作方式,它可能会成为你的编程工具箱中的重要组成部分。
|
Python
Python编程:Built-in Functions内建函数小结
Python编程:Built-in Functions内建函数小结
263 0
|
Python
Python 入门教程 11 ---- Lists and Functions
 第一节      1 介绍了列表list中的三种删除方法          1 my_list.pop(index)删除列表中下标为1的值,并返回这个值 my_list = [1, 3, 5] my_list.
1464 0
|
Python
Python编程:Built-in Functions内建函数小结
Python编程:Built-in Functions内建函数小结
306 0
|
13天前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
187 102
|
13天前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
187 104

推荐镜像

更多