本文主要针对比较火热的 mmdetection 的源代码进行解读说明,记录一下里面细节以及设计上的方案的优势。下文是本人的理解。持续更新。。。
class A :
def __init__(self,age, id=1):
self.age = age
self.id = id
@property
def age(self):
return self.__age
@age.setter
def age(self, age):
self.__age = age
@age.deleter
def age(self):
self.__age = None
obj = A(13)
print(obj.age)
obj.age = 12
print(obj.age)
del obj.age
print(obj.age) #AttributeError: A instance has no attribute '_A__age'
print getattr(obj, 'name', "xu") # 属性 name 不存在,但设置了默认值是xu
@functools.wraps
, 如果我们定义了函数装饰器,装饰一个函数,则这个函数的名字会改变,为了防止这种改变,一般要在装饰 def func(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
return wrapper
@func
def myfunc1():
print("myfunc1")
@func
def myfunc2():
print("myfunc2")
if __name__ == '__main__':
print(myfunc1.__name__) # wrapper
print(myfunc2.__name__) # wrapper
def wrapper(p1, *args, **kwargs):
print p1
print args
print kwargs
wrapper(1,2,3,4,a=3)
# 1
# (2,3,4)
# {'a':3}
# coding=utf-8
import os,sys
"""
python中以双下划线开始和结束的函数(不可自己定义)为魔法函数
python中每个魔法函数都对应与Python中的内置函数或对应操作,比如__str__ 对应str() __eq__ 对应 == __add__ 对应 +
比较函数
< object.__lt__(self, other)
<= object.__le__(self, other)
== object.__eq__(self, other)
!= object.__ne__(self, other)
>= object.__ge__(self, other)
> object.__gt__(self, other)
"""
class A():
"""docstring for ClassName"""
def __init__(self, name):
self.name = name
self.id = len(name)
def __eq__(self,other):
return self.name == other.name
def __add__(self,other):
return self.id + other.id
def __str__(self):
return "name : {} ,id : {}".format(self.name,self.id)
def __lt__(self,other):
return self.name < other.name if self.name != other.name else self.age < other.age
m=[1,2,3,4]
n=[1,2,3,4]
print m == n
a= A("aa")
b= A("aa")
print A("aa")
print a == b
print a+b
import os.path as osp
this_dir = osp.dirname(__file__) # 获取当前执行Python文件的目录,相对于当前目录
#python ./patent/test.py test_dir == ./patent