Python notes

记录一下python开发中用到的东西

求md5

def get_md5(raw_str):
    return hashlib.md5(raw_str.encode('utf-8')).hexdigest()

isinstance

一个例子:如果输入是object,直接append。如果是list,把每个元素append进来

all = []
def add_name(config):
    if isinstance(config,list):
        for v in config:
            all.append(v)
    elif isinstance(config,object):
        all.append(config)

add_name('aaa')
add_name(['bbb','ccc'])
print(all)

python模块管理

生成式和生成器

面向对象进阶

@property装饰器 getter setter

slots 限定类的属性,不允许动态绑定

静态方法 @staticmethod

类中定义类方法 @classmethod

类和类之间的关系有三种:is-a、has-a和use-a关系。

《UML面向对象设计基础》

© 皖ICP备20011981号