Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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 - Fatal编程技术网

Python 赋值前引用的局部变量

Python 赋值前引用的局部变量,python,Python,我是python新手,我找到了这个问题的一些答案,但没有什么能真正帮助我 下面是我遇到问题的代码部分: batch_index=0 # initializing a globale variable, I've tried with global batch_index too .................................. def next_Training_Batch(): if batch_index < len(train_data):

我是python新手,我找到了这个问题的一些答案,但没有什么能真正帮助我

下面是我遇到问题的代码部分:

batch_index=0 # initializing a globale variable, I've tried with global batch_index too 
..................................

def next_Training_Batch():
    if  batch_index < len(train_data):
        batch_index = batch_index + 1
        return train_data[batch_index-1], train_labels[batch_index-1]
    else :
        shuffle_data()
        batch_index = 0
        return train_data[batch_index], train_labels[batch_index]
我不想在函数中使用参数(如类似问题中所建议的),老实说,我使用的是“全局”变量的数量,没有任何错误,我不明白为什么不允许在if语句中对其求值?
谢谢你的提示

global batch_index
添加到函数的开头,它将知道您引用的是全局变量,而不是局部变量

batch_index=0 # initializing a globale variable, I've tried with global batch_index too 
...

def next_Training_Batch():
    global batch_index
    if batch_index < len(train_data):
        batch_index = batch_index + 1
        return train_data[batch_index - 1], train_labels[batch_index - 1]
    else:
        shuffle_data()
        batch_index = 0
        return tain_data[batch_index], train_labels[batch_index]
batch_index=0#初始化globale变量,我也尝试过使用global batch_index
...
def next_Training_Batch():
全局批处理索引
如果批次索引<长度(列数据):
批次索引=批次索引+1
返回序列号数据[批次号索引-1],序列号标签[批次号索引-1]
其他:
洗牌_数据()
批次索引=0
返回批次数据[批次索引],序列标签[批次索引]

使用创建变量副本的全局关键字

def next_Training_Batch():
    global batch_index
    if  batch_index < len(train_data):
        batch_index = batch_index + 1
        return train_data[batch_index-1], train_labels[batch_index-1]
    else :
        shuffle_data()
        batch_index = 0
        return tain_data[batch_index], train_labels[batch_index]
def next_Training_Batch():
全局批处理索引
如果批次索引<长度(列数据):
批次索引=批次索引+1
返回序列号数据[批次号索引-1],序列号标签[批次号索引-1]
其他:
洗牌_数据()
批次索引=0
返回批次数据[批次索引],序列标签[批次索引]

但是仍然没有定义训练数据

在函数中使用全局表示全局批量索引

batch_index=0#初始化globale变量时,我也尝试过使用global batch_index

def next_Training_Batch():
    global batch_index
    if  batch_index < len(train_data):
        batch_index = batch_index + 1
        return train_data[batch_index-1], train_labels[batch_index-1]
    else :
        shuffle_data()
        batch_index = 0
        return tain_data[batch_index], train_labels[batch_index]
def next_Training_Batch():
全局批处理索引
如果批次索引<长度(列数据):
批次索引=批次索引+1
返回序列号数据[批次号索引-1],序列号标签[批次号索引-1]
其他:
洗牌_数据()
批次索引=0
返回批次数据[批次索引],序列标签[批次索引]

谢谢摩西,我很挣扎!谢谢,不用担心,我知道问题已经回答了,但我有一个疑问。我在我的机器上尝试了一个示例代码。只有在执行赋值时才会发生错误,而只有在
if
条件下使用
batch\u索引时才会发生错误。为什么会这样?@Chris_Rands:试着给这个变量赋值,你应该会看到错误then@Abhimanyusingh您可以读取(甚至变异)全局变量,而无需函数中的
global
指令,但是,如果您在函数体中的任何位置对该名称执行赋值,则解释器会假定该名称引用的是局部变量,而不是全局变量。@chris_Rands我想是的,其余代码不会使用批处理索引或与函数BTW相关的任何内容,避免使用可修改的全局变量是个好主意:它们使代码模块化程度降低,因此更难调试和修改。@PM2Ring感谢您的提醒!
def next_Training_Batch():
    global batch_index
    if  batch_index < len(train_data):
        batch_index = batch_index + 1
        return train_data[batch_index-1], train_labels[batch_index-1]
    else :
        shuffle_data()
        batch_index = 0
        return tain_data[batch_index], train_labels[batch_index]