macrovve
4/20/2018 - 5:44 PM

Python Tips

Python Tips

logger 其实都是一个logger,但是我们使用的时候logger.info logger.debug 其实是由handler来进行处理
不要在模块上直接调用logging.getlogger(__name__)因为加载模块的时候,这个语句会先调用,然后导致你读取配置文件的时候没有办法
这个logger还没有配置,但是现在出现了disable_existing_loggers在fileconfig中就没有这个问题了
https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/


Logger对象从不直接实例化,而是通过模块级的功能logging.getLogger(name)创建Logger实例。调用 logging.getLogger(name) 功能时,如果传入的name参数值相同,则总是返回同一个Logger对象实例的引用。
https://blog.igevin.info/posts/python-log/


如果调用的都是logging.getLogger(__name__) 的话,其实就是一个logger,因为__name__的关系,会形成父子关系

1. 列表操作remove pop的区别

pop返回元素,remove不返回值

2. @classmethod @staticmethod的使用

  • Class method 一般当作工厂方法,来返回一个类,充当另外一种init,因为python没有重构函数
  • Static method 也可以返回一个类,但是继承之后就会出现问题,需要添加参数cls,可是这样之后,和class method一样了。 一般用来监测字符串 属性对不对
  1. 先看:Python 中的 classmethod 和 staticmethod 有什么具体用途? - 李保银的回答 讲明白了:@classmethod和@staticmethod是什么。
  2. 再看:oop - Python @classmethod and @staticmethod for beginner? 讲明白了:什么时候用、怎么用。
  3. 继续:oop - Python @classmethod and @staticmethod for beginner? 这个答案是对2的补充。讲明白了:为什么要这么用。