• mmdetection中的一些Python基础知识(super()/***args/**kwargs)


    1、super()和__init__()
    最常见调用方法: super(Class, self).init() 调用父类的初始化方法

    ***Python 3 和 Python 2 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :
    ***super().__init__相对于类名.init,在单继承上用法基本无差别
    ***但在多继承上有区别,super方法能保证每个父类的方法只会执行一次,而使用类名的方法会导致方法被执行多次。
    ***多继承时,使用super方法,对父类的传参数,应该是由于python中super的算法导致的原因,必须把参数全部传递,否则会报错
    ***单继承时,使用super方法,则不能全部传递,只能传父类方法所需的参数,否则会报错
    MMDetection中Python基础知识讲的很好:
    https://blog.csdn.net/weixin_45657478/article/details/126624962?spm=1001.2014.3001.5502
    super()用法讲的很好
    https://blog.csdn.net/qq_44804542/article/details/116173195

    2、Python中的*args和kwargs
    *args和kwargs是python的动态参数,其中args 是 arguments 的缩写,表示位置参数;kwargs 是 keyword arguments 的缩写,表示关键字参数。动态参数,必须放在所有的位置参数和默认参数后面,并且 *args 必须放在 **kwargs 的前面,因为位置参数在关键字参数的前面。
    类似地,在传参时,也可以使用 * 和 **,**会自动将传入的列表/元组以及字典分开。例如:

    def func(*args):
        print(args)
    func('lily', 22)  # 输出(lily', 22)
    
    def func(**kwargs):
        print(kwargs)
    func(gender='male',country='China')  # 输出{'gender': 'male', 'country': 'China'}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    def func(name, age,gender,country):
        print(name, age,gender,country)
    func_inputs = ['lily', 22]
    dict_arg={'gender':'male','country':'China'}
    func(*func_inputs,**dict_arg)  # 输出:lily 22 male China
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、pytorch中可能会遇见的不常用操作

    (1)None作为tensor索引,就是在指定的位置插入一个维度,相当于在相应的维度进行一次unsqueeze操作。

    a = torch.zeros(2,3)
    print(a[:,None,None].shape)   # 输出torch.Size([2,1,1,3])
    #类似作用的函数
    torch.unsqueeze(input,dim)->Tensor
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、mmdetection中的_base_和python中的__base__

    #mmdetection中的_base_,表明该配置文件继承自配置文件faster_rcnn_r50_fpn_1x_coco.py????????
    _base_ = './faster_rcnn_r50_fpn_1x_coco.py'
    #python中的__base__
    class A:
        def show(self):
            print('A')
    class B(A):
        def show(self):
            print('B')
    print(B.__base__)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5、python字典初始化

    dict1={'gender':'male','country':'China'}
    dict2=dict(gender='male', country='China')
    
    • 1
    • 2

    6、python__call__用法
    一个类实现一个特殊方法__call__ ,类的实例就可以变成一个可调用对象

    class Fruit():
        def __init__(self, name, price):
            self.name = name
            self.price = price
        def __call__(self, color):
            print('My price is %s...' % self.price)
            print('My color is %s...' % color)
    fruit=Fruit('apple','10')
    fruit('red')#fruit.__call__('red') 效果一样
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    【Vue】所有指令详解
    算法提升:图的拓扑排序算法
    使用kubasz快速搭建Kubernetes集群
    分销小程序开发|分销小程序怎么设计动态分销?
    宁夏果蔬系统
    GB-T 43698-2024 网络安全技术 软件供应链安全要求
    SwiftUI 状态管理系统指南
    如何使用Python实现发送邮件功能
    Bean的实例化(四)
    一名普通程序员的正确理财方式:建立思维模型,选中好公司,坚持长期主义
  • 原文地址:https://blog.csdn.net/m0_37737957/article/details/127566948