1.文档字符串(用于函数注释)使用help(函数名.__doc__())函数可以查看注释内容
def func(a,b): """ 用于比较两个数的大小 :param a: :param b: :return: """ if a>b: print("最大:%d"%a) else: print("最大:%d"%b) func(5,10) help(func.__doc__) # 查看注释内容
# No Python documentation found for '用于比较两个数的大小
:param a:
:param b:
:return:'.
# Use help() to get the interactive help utility.
# Use help(str) for help on the str class.
2.查看局部变量和全局变量,使用locals()和globals()
a = 3 def func(): b = 10 print(b*10) global a a = 300 print(locals()) # {'b': 10}
print(globals())
# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001FB793965C0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/代码库/Mysite/1-1.py', '__cached__': None, 'a': 300, 'func': <function func at 0x000001FB7934C268>}
func()
3.浅拷贝和深拷贝
浅拷贝不拷贝子对象
4.单例模式
class Singleton: __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = super().__new__(cls) # cls.__instance = object.__new__(cls) return cls.__instance s1 = Singleton() s2 = Singleton() print(id(s1)) # 2018028512928 print(id(s2)) # 2018028512928