• 多的是面向对象你不知道的事


    issubclass  用来检查 第一个参数是否是第二个参数的 子子孙孙类

    type 获取 当前的对象是又哪个类创建的 还可以导入 from types import Functiontype,Methodtype

    from types import FunctionType,MethodType

    isinstance(a,b)是检查第一个参数是都是第二个参数(类及父类)的实例

    用科学的方法判断是函数还是方法
    rom types import MethodType,FunctionType
    def check(arg):
        """
        检查arg是方法还是函数?
        :param arg:
        :return:
        """
        if isinstance(arg,MethodType):
            print('arg是一个方法')
        elif isinstance(arg,FunctionType):
            print('arg是一个函数')
        else:
            print('不知道是什么')
    #
    # def func():
    #     pass
    #
    # class Foo(object):
    #     def detail(self):
    #         pass
    #     @staticmethod
    #     def xxx():
    #         pass
    #
    #
    # check(func)
    #
    # obj = Foo()
    # check(obj.detail)
    # check(obj.xxx)
    
    # #### 特点
    """
    class Foo(object):
    
        def f1(self):
            pass
    
        def f2(self):
            pass
    
        def f3(self):
            pass
    
    # obj = Foo()
    # print(obj.f1)
    # print(obj.f2)
    
    obj = Foo()
    Foo.f1(obj) # 把f1当做函数
    
    obj = Foo()
    obj.f1()    # 把f1当做方法,自动传self值
    """
    
    # 练习1
    """
    class Foo(object):
    
        def f1(self):
            pass
    
        def f2(self):
            pass
    
        def f3(self):
            pass
    
        list_display = [f1,f2]
    
        def __init__(self):
            pass
    
    
    for item in Foo.list_display:
        item(123)
    """
    # 练习2
    # class Foo(object):
    #
    #     def f1(self):
    #         pass
    #
    #     def f2(self):
    #         pass
    #
    #     def f3(self):
    #         pass
    #
    #     list_display = [f1,f2]
    #
    # obj = Foo()
    # Foo.list_display.append(obj.f3)
    #
    # for item in Foo.list_display:
    #     print(item)
  • 相关阅读:
    用wamp配置的环境,想用CMD连接mysql怎么连
    Mysql删除表
    MySQL创建表
    Leetcode 130. Surrounded Regions
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 110. Balanced Binary Tree
    Leetcode 98. Validate Binary Search Tree
    Leetcode 99. Recover Binary Search Tree
    Leetcode 108. Convert Sorted Array to Binary Search Tree
    Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/zzy7372/p/9561679.html
Copyright © 2020-2023  润新知