Python3 locals()函数如何巧妙地应用在变量管理中?

2026-05-17 07:191阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计691个文字,预计阅读时间需要3分钟。

背景:今天在写一个小项目的时侯,发现项目初始阶段需要初始化一些目录。

发现:当时发现,若需要初始化的目录很多,而且这些目录操作都相同,需要判断目录是否存在,若不存在则创建。

结果:判断目录是否存在,若不存在则创建。

背景:今天在写一个小项目的时候,发现在项目初始的时候需要初始化一些目录,此时发现,如果需要初始化的目录很多;但是都是相同的代码操作,判断目录是否存在,如果不存在则创建;此时我就在想有没有一种方式,让系统自己去判断并创建,而只需要一次代码就可以实现;

def app_init_need_dir(): """ 初始化目录 :return: """ _app_root = os.path.dirname(os.getcwd()) cache = os.path.join(_app_root, 'cache') static = os.path.join(_app_root, 'static') logs = os.path.join(_app_root, 'logs') if not os.path.isdir(cache): os.mkdir(cache) if not os.path.isdir(static): os.mkdir(static) if not os.path.isdir(logs): os.mkdir(logs)

  于是去百度找了Python 函数如何获取函数的变量名称(此时我的思路是通过函数变量名称获取值);果然我找到了一个方式 func.__code__.co_varnames 通过这个魔法函数可以直接获取到函数的变量的名称,并且该方法还能放在函数去实现

def app_init_need_dir(): """ 初始化目录 :return: """ _app_root = os.path.dirname(os.getcwd()) cache = os.path.join(_app_root, 'cache') static = os.path.join(_app_root, 'static') logs = os.path.join(_app_root, 'logs') func_vars = app_init_need_dir.__code__.co_varnames print(func_vars)

由上图的执行结果可以看出,得出的结果是符合预期的,但是发现没有找到获取到变量的值;所有放弃了这种方式,后面找到 locals 方法,

def app_init_need_dir():
"""
初始化目录
:return:
"""
_app_root = os.path.dirname(os.getcwd())
cache = os.path.join(_app_root, 'cache')
static = os.path.join(_app_root, 'static')
logs = os.path.join(_app_root, 'logs')
for var, value in locals().items():
if '_' != var[0] and not os.path.isdir(value):
print(var, value)
os.mkdir(value)

locals()函数会以字典类型返回当前位置的全部局部变量。

对于函数, 方法, lambda 函式, 类, 以及实现了 __call__ 方法的类实例, 它都返回 True。

最后附上一种更加简单的方式(适合路径都一样的情况)

def app_init_need_dir(): """ 初始化目录 :return: """ _app_root = os.path.dirname(os.getcwd()) for _dir in ['cache', 'static', 'logs']: path = os.path.join(_app_root, _dir) if not os.path.isdir(path): os.mkdir(path)

本文共计691个文字,预计阅读时间需要3分钟。

背景:今天在写一个小项目的时侯,发现项目初始阶段需要初始化一些目录。

发现:当时发现,若需要初始化的目录很多,而且这些目录操作都相同,需要判断目录是否存在,若不存在则创建。

结果:判断目录是否存在,若不存在则创建。

背景:今天在写一个小项目的时候,发现在项目初始的时候需要初始化一些目录,此时发现,如果需要初始化的目录很多;但是都是相同的代码操作,判断目录是否存在,如果不存在则创建;此时我就在想有没有一种方式,让系统自己去判断并创建,而只需要一次代码就可以实现;

def app_init_need_dir(): """ 初始化目录 :return: """ _app_root = os.path.dirname(os.getcwd()) cache = os.path.join(_app_root, 'cache') static = os.path.join(_app_root, 'static') logs = os.path.join(_app_root, 'logs') if not os.path.isdir(cache): os.mkdir(cache) if not os.path.isdir(static): os.mkdir(static) if not os.path.isdir(logs): os.mkdir(logs)

  于是去百度找了Python 函数如何获取函数的变量名称(此时我的思路是通过函数变量名称获取值);果然我找到了一个方式 func.__code__.co_varnames 通过这个魔法函数可以直接获取到函数的变量的名称,并且该方法还能放在函数去实现

def app_init_need_dir(): """ 初始化目录 :return: """ _app_root = os.path.dirname(os.getcwd()) cache = os.path.join(_app_root, 'cache') static = os.path.join(_app_root, 'static') logs = os.path.join(_app_root, 'logs') func_vars = app_init_need_dir.__code__.co_varnames print(func_vars)

由上图的执行结果可以看出,得出的结果是符合预期的,但是发现没有找到获取到变量的值;所有放弃了这种方式,后面找到 locals 方法,

def app_init_need_dir():
"""
初始化目录
:return:
"""
_app_root = os.path.dirname(os.getcwd())
cache = os.path.join(_app_root, 'cache')
static = os.path.join(_app_root, 'static')
logs = os.path.join(_app_root, 'logs')
for var, value in locals().items():
if '_' != var[0] and not os.path.isdir(value):
print(var, value)
os.mkdir(value)

locals()函数会以字典类型返回当前位置的全部局部变量。

对于函数, 方法, lambda 函式, 类, 以及实现了 __call__ 方法的类实例, 它都返回 True。

最后附上一种更加简单的方式(适合路径都一样的情况)

def app_init_need_dir(): """ 初始化目录 :return: """ _app_root = os.path.dirname(os.getcwd()) for _dir in ['cache', 'static', 'logs']: path = os.path.join(_app_root, _dir) if not os.path.isdir(path): os.mkdir(path)