Python 函数未正确执行定义的全局变量

Python Function is not performing properly with a Global Variable defined

这是我的函数.........

from instrument import Instrument
import pandas as pd

  def getpairsData():
    global pairlist    <<============= Defined Global variable
    pairlist = Instrument.get_pairs_from_list() <<=============Call
    return pairlist   <<============= Available Global variable
 

"""正在被这个循环调用....... #这个循环会遍历pairlist,传递一对来获取数据,并将数据保存为文件。"""

 for pair in pairlist:      <<======= My Global variable above????

    getData()
    for i in range(0,11):
        df1= pd.read_csv("./data/"+ str(files[i])+".csv")
        print (df1.head())



import pandas as pd
import Utils

class Instrument():
def __init__(self,ob):
    self.name=ob['name']
    self.type=ob['type']
    self.displayName = ob['displayName']
    self.pipLocation = pow(10,ob['pipLocation']) # ex. - --> 0.0001
    self.marginRate = ob['marginRate']


@classmethod
def get_pairs_from_list(cls):  <<============= Receive Call 1
    i_list = cls.get_instruments_list()
    i_keys = [x.name for x in i_list]

    df = pd.DataFrame(i_keys)
    df = df.rename(columns={0: "Names"})

    df['Names'] = df['Names'].str.replace("_", '')
    print("You are here")

    pairlist = df["Names"].tolist()  # if it is being changed to a list with no keys

    return pairlist   <<============= Return Call 1

"""我认为 pairlist 会 return 到设置为全局变量的原始函数并关闭我们去的比赛,但我被困在印刷品上(“你在这里") 完全没有响应。

NameError: 名称 'pairlist' 未定义"""

因为我不了解你的实现的全貌,但我认为你的代码应该类似于下面的代码片段:

from instrument import Instrument
import pandas as pd

def getpairsData():
  global pairlist
  pairlist = Instrument.get_pairs_from_list() # <<=============Call
  return pairlist   # <<============= Available Global variable
 
pairlist = getpairsData()
 
for pair in pairlist:      # <<======= My Global variable above????
  for i in range(0,11):
    df1= pd.read_csv("./data/"+ str(files[i])+".csv")
    print (df1.head())



import pandas as pd
import Utils

class Instrument():

  def __init__(self,ob):
      self.name=ob['name']
      self.type=ob['type']
      self.displayName = ob['displayName']
      self.pipLocation = pow(10,ob['pipLocation']) # ex. - --> 0.0001
      self.marginRate = ob['marginRate']


  @classmethod
  def get_pairs_from_list(cls):  # <<============= Receive Call 1
      i_list = cls.get_instruments_list()
      i_keys = [x.name for x in i_list]

      df = pd.DataFrame(i_keys)
      df = df.rename(columns={0: "Names"})

      df['Names'] = df['Names'].str.replace("_", '')
      print("You are here")

      pairlist = df["Names"].tolist()  # if it is being changed to a list with no keys

      return pairlist   # <<============= Return Call 1
"""I have found that  @tdelaney explanation was correct.  I never called 
the variable because it was derived and then needed to be returned."""

@classmethod
def get_pairs_from_list(cls):  <<============= Receive Call 1
    i_list = cls.get_instruments_list()
    i_keys = [x.name for x in i_list]

    df = pd.DataFrame(i_keys)
    df = df.rename(columns={0: "Names"})

    df['Names'] = df['Names'].str.replace("_", '')
    print("You are here")

    pairlist = df["Names"].tolist()  # if it is being changed to a list 
                                    with no keys

    `return getpairsData(pairlist)`   <<=========== Return Call with 
    Variable