Python - 如何获取特定范围内变量的字典(甚至列表);比 locals/globals() 更具体

标签 python dictionary scope global locals

所以,标题几乎说明了一切。

例如让我们看下面的示例代码:

## How can I obtain a dict/list (like locals()) of all the variables in second and/or third layer scopes via a command 
# coming from the first layer?
## Or another example would be how could I obtain the variables "locals() style" from the thirdlayer via a 
# command from the second layer?
# essentially can a parent function/class access a list/dict of a child function
# or class??

def firstLayer():
     a = 4.7
     q = locals()
     print(q)
     # local vars of 1st layer
     def secondlayer():
          b = 7
          r = locals()
          print(r)
          # local vars of 2nd layer
          def thirdlayer():
               c = False
               s = locals()
               i = globals()
               print('c:\n', c, "\nglobals from 3rd layer:\n\t", i)
              # local vars of 3rd layer
          thirdlayer()

     secondlayer()
firstLayer()

sample_var = globals()
print(sample_var)
# returns the list of global variables

重申我在代码的注释中所说的内容,本质上是他们可以通过任何方式获取“子”范围的所有局部变量的列表?我知道功能已关闭,但如果他们无法做到这一点,他们可以使用任何更复杂的代码来实现这一点,如果需要,我可以将其集成到一个函数或类中。

编辑: 进一步阐述;这就是我所处的情况。

def varsfunc():
    font1 = "Harlow Solid"
    grey = '#454545'
    font2 = 'Mistral'
    font3 = 'Italic 35px Times New Roman'
    pnk = 'pink'
    grn = 'green'
    return locals()

本质上,我正在创建一个模块,用户必须创建某种类型的函数,他们列出他们想要声明的所有变量以用于修改 css 文件。本质上,我想让用户不必键入“return locals()”。我想通过让最终用户将上面的示例函数包装在一个装饰器中来实现它,该装饰器相当于返回我想要的确切范围的 locals() 。装饰器对我不起作用,因为它在外部范围内。

更清楚: 我需要一个包装另一个函数(即装饰器)的装饰器/函数,它可以访问和创建子元素列表。

def module_decorator_func_thing():
     r = command_that_acts_like_locals()_but_for_child_scopes
     def user_var_list():
          font1 = 'green'
          font2 = 'pink'
     # back in "module_decorator_func_thing"'s scope
     print(r) # this variable should contain only a dict/list containing the
     # the following:
     # r = {'font1': 'green', 'font2': 'pink')

目前用户需要这样做:

def vars_func_container():
     font1 = 'green'
     font2 = 'pink'
     return locals() #  <---- I want the user to not have to type this and for 
    # a function decorator to take care of it instead possibly.

@aguy 和其他希望了解更多信息的人的信息。 我通过你们的提示获得的字典/列表将被发送到这个函数来完成程序的真正工作。 (如果我要开始使用列表,我需要转换成字典,但这对我来说没问题。) 变量字典与此函数一起用于“编译/编译”(双关语“Python”+“编译”)并插入“变量”参数中。例如你像这样执行函数。

compyle("My sample title", return_stylesheet_from_func(*insert .css filename), 
return_variables_from_function(*insert function containing variables*), "**True/False to turn on compilation**", 
"**True/False to turn on annotations/suggestions**")

def compyle(title, style_sheet, variables, boolean=False, boolean2=True):
    """
    :param title: The name you wish your .css file to be named.
    :param style_sheet: The name of the multi-line string that will compose your .css file
    :param variables: The name of the dictionary containing your .pcss variables
    :param boolean: A.K.A the "Compiler Parameter" - Turns the compiler on or off
    :param boolean2: A.K.A the "Annotation Parameter" - Turns annotations on or off
    :return: returns compiled .pcss text as normal .css style text to be utilized with .html
    """
    # -----------------------------------
    file_name = title + ".css"
    replace_num = len(variables.keys())
    counter = replace_num
    content = style_sheet
    # -----------------------------------
    # add theme support with namedtuple's formatted to mimic structs in C/C++
    # this will be a major feature update as well as a nice way to allow the future prospect of integrating C/C++ into
    # the compiler. Info: https://stackoverflow.com/questions/35988/c-like-structures-in-python
    for k, v in variables.items():
        counter -= 1
        content = content.replace(k, v, replace_num)
        if counter == 0:
            break
        else:
            pass
    looped_content = str(content)
    id_content = looped_content.replace("hash_", "#")
    output = id_content.replace("dot_", ".")

    if boolean is True:
        if boolean2 is True:
            output = " /* --- Pyle Sheet --- */\n" + output
            with open(file_name, 'w') as writ:
                writ.write(output)
                writ.close()
                print('compiled successfully; The file was saved as ' + "\"" + file_name + "\".")
        elif boolean2 is False:
            pass
        else:
            logging.warning("An Error Occurred - see module, documentation, or online Q&A for assistance.")
    elif boolean is False:
        if boolean2 is True:
            print('compiled successfully; The file ' + "\"" + file_name + "\"" + "was not saved/created.")
        elif boolean2 is False:
            pass
        else:
            logging.warning("An Error Occurred - see module, documentation, or online Q&A for assistance.")
    else:
        logging.warning('An Error Occurred with the Compile Parameter (See: boolean in pyle_sheets source file) - \ '
                        'see module, documentation, or online Q&A for assistance.')

最佳答案

如果不深入研究,我看不出有什么方法可以做到这一点;以下是我想到的最简单的解决方案。

它是如何工作的

使用 ast模块,我们遍历给定函数的代码并找到所有分配。这些在给定的命名空间中进行评估,并返回此命名空间。

代码

import ast
import functools
import inspect

def returnAssignments(f):
    @functools.wraps(f)
    def returner():
        assignments = dict()
        for node in ast.walk(ast.parse(inspect.getsource(f))):
            if isinstance(node, ast.Assign):
                exec(compile(ast.Module([node]), '<ast>', 'exec'),
                     globals(),
                     assignments)
        return assignments
    return returner

用法

from ra import returnAssignments

@returnAssignments
def foo():
    this = 'something'
    that = 37
    the_other = object()

print(foo())

输出

rat@pandion:~/tmp$ python test.py
{'this': 'something', 'that': 37, 'the_other': <object object at 0x10205b130>}

关于Python - 如何获取特定范围内变量的字典(甚至列表);比 locals/globals() 更具体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48838029/

相关文章:

javascript - 使用 Flask 变量作为 highcharts 的数据源

python - 使用 Appengine 的导航台从实体的 ID 获取 key

python - 在 A 列中搜索字典键的文本,如果找到则将 B 列设置为 Pandas 数据框中的字典值

python dict,列表对所有类都有相同的实例

对C中变量的范围感到困惑

Python 列表到列表列表

python - basemap 导致python中止

Java:根据内部映射对Map进行排序,Map<Long,Map<String, Data>>

java - 我无法在一个 View 中将数据存储在对象中并从另一个 View 访问它(其中包含数据)

c++ - 在 C++ 中,函数是否可以访问直接外部作用域中的变量,而无需向函数输入参数?