zoukankan      html  css  js  c++  java
  • 一些内置的魔术方法

    一些内置的魔术方法

    _call_ : 对象() 调用这个类中的__call__方法

    class A:
        def __call__(self, *args, **kwargs):
            print('_________')
    obj = A()
    print(callable(obj))
    obj()
    A()() #obj = A() ==>obj()
    
     __len__  : len(对象) 需要实现这个,类中加__len__方法
    
    class Cls:
        def __init__(self,name):
            self.name = name
            self.students = []
        def len(self):
            return len(self.students)
        def __len__(self):
            return len(self.students)
    py22 = Cls('py22期')
    py22.students.append('大壮')
    py22.students.append('alex')
    print(py22.len())
    print(len(py22))
    
    ---------------------
    class Pow:
        def __init__(self,n):
            self.n = n
        def __pow2__(self):
            return self.n ** 2
    
    def pow2(obj):
        return obj.__pow2__()
    
    obj = Pow(10)
    print(pow2(obj))
    

    __new:

    实例化的时候

    先创建一块对象的空间,有一个指针能指向类 --> new

    调用init --> init

    class A:
        def __new__(cls, *args, **kwargs):
            o = object.__new__(cls)
            print('执行new',o)
            return o
        def __init__(self):
            print('执行init',self)
    A() 
    # 执行new <__main__.A object at 0x00000000022802C8>
    # 执行init <__main__.A object at 0x00000000022802C8>
    

    设计模式 -- 单例模式

    # 一个类 从头到尾 只会创建一次self的空间
    class Baby:
        __instance = None  #标识
        def __new__(cls, *args, **kwargs):
            if cls.__instance is None:
                cls.__instance = super().__new__(cls)
            return cls.__instance
        def __init__(self,cloth,pants):
            self.cloth = cloth
            self.pants = pants
    b1 = Baby('红毛衣','绿皮裤')
    print(b1.cloth)  #红毛衣
    b2 = Baby('白衬衫','黑豹纹')
    print(b1.cloth)  #白衬衫
    print(b2.cloth)  #白衬衫
    

    _str_:帮助我们在打印展示对象的时候更直观的显示对象内容 %s str() print()

    _repr_:repr是str的备胎,同时还和%r和repr有合作关系

    class clas:
        def __init__(self):
            self.student = []
        def append(self,name):
            self.student.append(name)
        def __str__(self):
            return str(self.student)
    #
    py22 = clas()
    py22.append('大壮')
    print(py22)
    print(str(py22))
    print('我们py22班 %s'%py22)
    print(py22)
    py22.append('大壮')
    print(py22)
    # ['大壮']
    # ['大壮']
    # 我们py22班 ['大壮']
    # ['大壮']
    # ['大壮', '大壮']
    # 在打印一个对象的时候 调用__str__方法
    # 在%s拼接一个对象的时候 调用__str__方法
    # 在str一个对象的时候 调用__str__方法
    
    class clas:
        def __init__(self):
            self.student = []
        def append(self,name):
            self.student.append(name)
        def __repr__(self):
            return str(self.student)
        def __str__(self):
            return 'aaa'
    
    py22 = clas()
    py22.append('大壮')
    print(py22)
    print(str(py22))
    print('我们py22班 %s'%py22)
    print('我们py22班 %r'%py22)
    print(repr(py22))
    # aaa
    # aaa
    # 我们py22班 aaa
    # 我们py22班 ['大壮']
    # ['大壮']
    
    # 当我们打印一个对象 用%s进行字符串拼接 或者str(对象)总是调用这个对象的__str__方法
    # 如果找不到__str__,就调用__repr__方法
    # __repr__不仅是__str__的替代品,还有自己的功能
    # 用%r进行字符串拼接 或者用repr(对象)的时候总是调用这个对象的__repr__方法
    
  • 相关阅读:
    微信小程序HTTPS
    微信商城-1简介
    va_list
    Event log c++ sample.
    EVENT LOGGING
    Analyze Program Runtime Stack
    unknow table alarmtemp error when drop database (mysql)
    This application has request the Runtime to terminate it in an unusual way.
    How to check if Visual Studio 2005 SP1 is installed
    SetUnhandledExceptionFilter
  • 原文地址:https://www.cnblogs.com/wyh0717/p/13246208.html
Copyright © 2011-2022 走看看