zoukankan      html  css  js  c++  java
  • 面向对象——类的内置方法(魔法方法)

    面向对象——类的内置方法(魔法方法)

    什么是类的内置方法?

    凡是在类内部定义,以 __ 开头,以 __结尾的方法都是类的内置方法,也称之为魔法方法

    类的内置方法会在某种条件满足下自动触发

    常用的内置方法

    __ init __()

    在调用类时自动触发。 通过产生的对象自动调用__ init__()

    class Demo(object):
    	def __init__(self):
        print("此处是__init__方法...")
    deom_obj = Demo()	# 此处是__init__方法...
    

    __ new__(cls, *args, **kwargs)

    在__ init __触发前,自动触发

    class Demo(object):
    
        def __init__(self):
            print("此处是__init__方法...")
    
        # __new__;在__init__触发前,自动触发
        def __new__(cls, *args, **kwargs):
            print("此处是__new__方法..")
            # Python内部通过object调用内部的__new__实现产生一个空对象
            return object.__new__(cls, *args, **kwargs)
        
    demo_obj = Demo()
    # 打印结果:
    # 此处是__new__方法..
    # 此处是__init__方法...
    

    __ getattr __(self, item)

    在 “对象.属性” 获取属性时,“属性没有” 时触发

    class Demo(object):   
        # 在 “对象.属性” 获取属性时,属性没有时触发
        def __getattr__(self, item):
            print("此处是__getattr__方法...")
            return 'Boom'   # return 想要返回的值
    deom_obj = Demo()
    print(deom_obj.x)
    # 打印结果:
    # 此处是__getattr__方法...
    # Boom
    

    __ getattribute __(self, item)

    在“对象.属性”获取属性时,有没有属性都会触发

    class Demo(object):
        # 在“对象.属性”获取属性时,无论属性“有没有”都会触发
        def __getattribute__(self, item):
            print("此处是__getattribute__方法...")
            return item
        
    deom_obj = Demo(5)
    print(deom_obj.x)
    # 打印结果:
    # 此处是__init__方法...
    # 此处是__getattribute__方法...
    # x
    

    __ setattr __(self, key, value)

    当“对象.属性 =属性值 ” 添加或修改属性时触发

    class Demo(object):
    
        def __init__(self, x):
            self.x = x
            print("此处是__init__方法...")
            
        def __setattr__(self, key, value):
            print("此处是__setattr__方法...")
            print(key, value)
            # 此处是对对象的名称空间---> 字典进行操作
            self.__dict__[key] = value
            print(key, value)
    
    deom_obj = Demo(5)
    deom_obj.y = 10
    # 打印结果:
    # 此处是__setattr__方法...
    # x 5
    # x 5
    # 此处是__init__方法...
    # 此处是__setattr__方法...
    # y 10
    # y 10
    

    __ call __(self, *args, **kwargs)

    在“对象+()”调用对象时触发

    class Demo(object):
        def __call__(self, *args, **kwargs):
            print("此处是__call__方法...")
            # 调用对象时的返回值
            return [1, 2, 3]
    deom_obj = Demo()
    print(deom_obj())
    # 打印结果:
    # 此处是__call__方法...
    # [1, 2, 3]
    

    __ str __(self)

    在打印对象时触发

    class Demo(object):
        def __str__(self):
            print("此处是__str__方法...")
            return "111"
    
    deom_obj = Demo()
    print(deom_obj)
    # 打印结果:
    # 此处是__str__方法...
    # 111
    

    __ getitem __(self, item)

    在对象通过“对象[key]”获取属性时触发

    class Demo(object):
        # 在对象通过“对象[key]”获取属性时触发
        def __getitem__(self, item):
            print("此处是__getitem__方法...")
            print(item)
            return self.__dict__[item]
    
    deom_obj = Demo()
    deom_obj.x = 10
    print(deom_obj["x"])
    # 打印结果:
    # 此处是__getitem__方法...
    # x
    # 10
    

    __ setitem __(self, key, value)

    在对象通过“对象[key] = value值”设置属性时触发

    class Demo(object):
        # 在对象通过“对象[key] = value值”设置属性时触发
        def __setitem__(self, key, value):
            print("此处是__setitem__方法...")
            print(key, value)
            self.__dict__[key] = value
            
    deom_obj = Demo()
    deom_obj["x"] = 20
    # 打印结果:
    # 此处是__setitem__方法...
    # x 20
    
  • 相关阅读:
    基本语句
    mysql多表查询方法(join)
    MySQL JOIN 多表连接
    MySQL SHOW INDEX语法的实际应用
    1.索引作用
    MySQL索引和优化查询
    mysql复合索引、普通索引总结
    mysql 索引相关
    for循环的break和continue
    保护程序猿滴眼睛---修改VS 2012 编辑器颜色
  • 原文地址:https://www.cnblogs.com/aheng/p/11959325.html
Copyright © 2011-2022 走看看