zoukankan      html  css  js  c++  java
  • python进阶之函数和类内建魔法属性

    前言

    关于对象的魔法方法我们已经讲得太多,但是对于类或函数内建的魔法属性和功能我们涉及较少,下面系统了解一下类和函数的内建属性。

    查看内建属性

    class Person(object):
        pass
    
    def get_name():
        pass
    
    if __name__ == "__main__":
        person = Person()
        print(dir(get_name))
        print(dir(Person))
        print(dir(person))
    # 结果
    ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
    

    比较一下发现类和它的实例内建的属性和方法是一样的,函数似乎比类的内建的属性和方法更多,分离出属性:

    • 类的内建属性有:
    '__class__', '__dict__', '__doc__', '__module__', '__weakref__'
    
    • 函数的内建属性有:
    '__annotations__', '__class__', '__closure__', '__code__', '__defaults__', '__dict__','__doc__', '__kwdefaults__', '__module__', '__name__','__qualname__',__globals__
    

    前面我们基本介绍过来类的内建属性,现在重点针对函数的内建属性。

    函数的内建属性

    内建属性是不能在函数的作用域内直接使用的,因为它们没有被显性地定义,它们需要通过函数变量来调用。

    • _annotations_

    记录函数的参数和返回值的类型,前提是定义时指定了类型,否则为{}.

    def get(x:int, y:int) -> int:
        return x + y
    
    print(get.__annotations__) # {'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}
    
    • _class_

    python万物皆对象,所以函数也是一个对象,可以看做简化版的类的实例,它的类对象就是FunctionType。

    def get(x:int, y:int) -> int:
        return x + y
    print(get.__class__) # <class 'function'>
    
    • __defaults__和__kwdefaults__

    __defaults__属性用来存储参数的默认值,是一个元组,如果没有默认值为None。

    def get(x:int=1, y:int=3) -> int:
        return x + y
    print(get.__defaults__) # (1, 3)
    

    __kwdefaults__记录强制关键字参数的默认值,字典形式,没有置为None。

    def get(x:int, y:int, *, age=20):
        pass
    print(get.__kwdefaults__) # {'age':20}
    
    # 函数*后面的参数表示强制关键字参数,即一定要:
    get(3,4,age=30)这种方式调用
    
    • __name__和_qualname_

    __name__和__qualname__分别表示函数的名字和合法名,__name__仅仅是函数名字,而__qualname__会用点示法显示函数所在的类和模块。

    • __doc__和_module_:和类的该属性一样,__doc__记录该函数的说明,__module__表示函数定义的模块的名字。

    • _globals_:函数定义所在模块的全局命名空间的字典的引用。

    • _closure_:以包含cell的元组形式返回闭包所包含的自由变量。

    def name(x=2):
        c = 0
        def name1(y):
            return x + y + c
        return name1
    n = name()
    print(n.__closure__)
    # 结果
    (<cell at 0x000001B1B201EEB8: int object at 0x00000000619760C0>, 
    <cell at 0x000001B1BAEFB768: int object at 0x0000000061976100>)
    # 这里的自由变量指的是y和c
    
    • _dict_

    既然函数也是对象,那么它也应该有自己的属性,默认为{}.

    def get(x:int=1, y:int=3) -> int:
        return x + y
    get.name = 'get'
    print(get.__dict__) # {'name':'get'}
    
    • _code_:返回已编译的函数对象,CodeType对象。

    总结

    1. 以上是普通的自定义函数的内建属性,它和生成器函数、协同函数的概念是不一样的;

    2. 对于普通的业务开发来说,需要用到的内建属性一般为:__defaults__,__name__,__doc__.

    参考

  • 相关阅读:
    RadGrid Expand/Collapse on Row click
    AutoComplete Textbox with Additional Parameters From Database
    Combobox.Items中添加项Items
    JavaScript 处理字符串(操作字符串)
    用nettiers + svn + resharper + rad + ccNet开发前的准备工作
    Document.location.href和.replace的区别
    .net remoting的事务传播以及wcf分布式事务
    IDA反汇编/反编译静态分析iOS模拟器程序(三)函数表示与搜索函数
    [置顶] 一道有趣的逻辑题
    mini2440uboot移植基本操作指令
  • 原文地址:https://www.cnblogs.com/cwp-bg/p/9856987.html
Copyright © 2011-2022 走看看