Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
脚本中的Python变量范围问题_Python_Scope - Fatal编程技术网

脚本中的Python变量范围问题

脚本中的Python变量范围问题,python,scope,Python,Scope,我遇到了一些奇怪的情况,变量在设置后在其他函数中无法访问。这是一个名为html.py base_path = '' @app.task(bind=True) def status(self): """ returns the count of files downloaded and the timestamp of the most recently downloaded file """ num_count = 0 latest_timestamp

我遇到了一些奇怪的情况,变量在设置后在其他函数中无法访问。这是一个名为
html.py

base_path = ''

@app.task(bind=True)
def status(self):
    """
    returns the count of files downloaded and the timestamp of the most recently downloaded file
    """

    num_count = 0
    latest_timestamp = ''
    for root, _, filenames in os.walk(base_path):
        for filename in filenames:
            file_path = root + '/' + filename
            file_timestamp = datetime.fromtimestamp(os.path.getctime(file_path))
            if latest_timestamp == '' or file_timestamp > latest_timestamp:
                latest_timestamp = file_timestamp
            num_count += 1

@app.task(bind = True)
def download(self, url='', cl_id=-1):
    if len(url) == 0 or cl_id < 0:
        return None

    base_path = settings.WGET_PATH + str(cl_id)

    log_paths = {
        'output' : wget_base_path + '/out.log',
        'rejected' : wget_base_path + '/rejected.log'
    }

    create_files(log_paths)
    wget_cmd = 'wget -prc --convert-links --html-extension --wait=3 --random-wait --no-parent ' \
                   '--directory-prefix={0} -o {1} --rejected-log={2} {3}'.\
        format(wget_base_path, log_paths['output'], log_paths['rejected'], url)

    subprocess.Popen(wget_cmd, shell = True)
wget过程按预期启动。但是,文件顶部的
base\u path
参数从未设置,因此当我通过调用
status

status = html.status.delay()

base\u path
变量是一个空字符串,尽管在
download
之后调用了
status
。这是因为这些任务是在脚本中还是在类中?

因为在函数
下载中,这一行

base_path = settings.WGET_PATH + str(cl_id)
您只需创建一个名为
base\u path
的局部变量。为了避免这种情况,您应该在函数中将
base\u path
声明为
global
。例如:

@app.task(bind = True)
def download(self, url='', cl_id=-1):
    if len(url) == 0 or cl_id < 0:
        return None

    global base_path
    base_path = settings.WGET_PATH + str(cl_id)
...
@app.task(bind=True)
def下载(self,url='',cl_id=-1):
如果len(url)==0或cl_id<0:
一无所获
全局基路径
基本路径=settings.WGET路径+str(cl\u id)
...
来自Python:

在执行期间的任何时候,至少有三个嵌套作用域的名称空间可以直接访问:

  • 最里面的范围(首先搜索)包含本地名称
  • 从最近的封闭作用域开始搜索的任何封闭函数的作用域都包含非局部名称,但也包含非全局名称
  • 倒数第二个作用域包含当前模块的全局名称
  • 最外层的作用域(最后搜索)是包含内置名称的命名空间

如果一个名称被声明为全局的,那么所有引用和赋值都直接进入包含模块全局名称的中间作用域。否则,在最内层作用域之外找到的所有变量都是只读的(尝试写入这样的变量只会在最内层作用域中创建一个新的局部变量,保持同名的外部变量不变)。

Hmm,ok。谢谢你的解释。Java的另一个实例是Python,让我大吃一惊。
@app.task(bind = True)
def download(self, url='', cl_id=-1):
    if len(url) == 0 or cl_id < 0:
        return None

    global base_path
    base_path = settings.WGET_PATH + str(cl_id)
...