python function at 0x00000_Python 自定义函数的特殊属性(收藏专用)

Python 中通过函数定义所创建的用户自定义函数对象均具有一些特殊属性,需要注意的是这里介绍的是自定义函数(function类型)的特殊属性,而非方法(method 类型)的特殊属性,函数和方法的特熟属性以及默认的返回值可能不尽相同。

对于大多数特殊属性,可以通过下面这个例子示范一下:

class Test():

def func(self, v = 'dog'):

'''这里演示一个闭包函数'''

name = 'dobi'

def inn_func(age = 1):

print(name, v, age)

return inn_func

test = Test()

clsfunc = test.func()

首先看一下方法与函数的区别:实例的函数为bound method,而类的函数以及闭包均为function,需要强调的是 Python 2.x 中类的函数为unbound method,这点与Python 3.x 有所不同,本文则基于 Python 3.51 整理。

print(Test.func)

#

print(test.func)

# >

print(clsfunc)

# .inn_func at 0x0000020F071D7F28>

__doc__

可写;用于获取函数的文档说明,如果没有,则返回 None。

print('Test.func.__doc__:', Test.func.__doc__)

# Test.func.__doc__: 这里演示一个闭包函数

Test.func.__doc__ = 'ddd' #注意,这里是 Test,不能是 test

print('Test.func.__doc__:', Test.func.__doc__)

# Test.func.__doc__: ddd

__name__

可写;获取函数的名称。

print('Test.func.__name__:', Test.func.__name__)

# Test.func.__name__: func

Test.func.__name__ = 'pet'

print('Test.func.__name__:', Test.func.__name__)

# Test.func.__name__: pet

__qualname__

可写;获取函数的qualname:点示法显示函数名称、所在的类、模块等梯级地址。

print('Test.func.__qualname__:', Test.func.__qualname__)

# Test.func.__qualname__: Test.func

Test.func.__qualname__ = 'path'

print('Test.func.__qualname__:', Test.func.__qualname__)

# Test.func.__qualname__: path

__module__

可写;返回函数所在的模块,如果无则返回None。

print('Test.func.__module__:', Test.func.__module__)

# Test.func.__module__: __main__

Test.func.__module__ = 'a'

print('Test.func.__module__:', Test.func.__module__)

# Test.func.__module__: a

__defaults__

可写;以元组的形式返回函数的默认参数,如果无默认参数则返回None。

print('Test.func.__defaults__:', Test.func.__defaults__)

# Test.func.__defaults__: ('dog',)

Test.func.__defaults__ = ('cat',)

print('Test.func.__defaults__:', Test.func.__defaults__)

# Test.func.__defaults__: ('cat',)

print('clsfunc.__defaults__:', clsfunc.__defaults__)

# clsfunc.__defaults__: (1,)

clsfunc.__defaults__ = (2,)

print('clsfunc.__defaults__:', clsfunc.__defaults__)

# clsfunc.__defaults__: (2,)

__code__

可写;返回已编译的函数对象。

print('Test.func.__code__:', Test.func.__code__)

# Test.func.__code__:

def func2():print('cat')

Test.func.__code__ = func2.__code__

Test.func()

# cat

print('Test.func.__code__:', Test.func.__code__)

# Test.func.__code__:

__globals__

只读,以字典的形式返回函数所在的全局命名空间所定义的全局变量。

print('Test.func.__globals__:', Test.func.__globals__)

# Test.func.__globals__: {

# '__cached__': None,

# 'Test': ,

# '__builtins__': ,

# 'func2': ,

# '__spec__': None,

# '__doc__': None,

# '__file__': 'D:\\...\\a.py',

# 'test': <__main__.test object at>,

# 'clsfunc': .inn_func at 0x0000020F071D7F28>,

# '__package__': None,

# '__name__': '__main__',

# '__loader__': <_frozen_importlib_external.sourcefileloader object at>

# }

__dict__

可写;以字典的形式返回命名空间所支持的任意自定义的函数属性。

print('Test.func.__dict__:', Test.func.__dict__)

# Test.func.__dict__: {}

__closure__

只读;以包含cell的元组形式返回闭包所包含的自由变量。

print('Test.func.__closure__:', Test.func.__closure__)

# None

print('clsfunc.__closure__:', clsfunc.__closure__)

# clsfunc.__closure__: (

# ,

#

# )

print('clsfunc.__closure__[x]:', clsfunc.__closure__[0].cell_contents, clsfunc.__closure__[1].cell_contents)

# clsfunc.__closure__[x]: dobi dog

__annotations__

__kwdefaults__

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值