Python函数在定义了全局变量的情况下运行不正常

2024-07-08 16:09:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的功能

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会返回到设置为全局变量的原始函数,然后返回到我们进行的比赛,但我被困在打印上(“您在这里”)根本没有响应

NameError:未定义名称“pairlist”


Tags: fromimportselfdfgetnamesdefcall
2条回答

因为我没有您的实现的完整视图,但我认为您的代码应该类似于下面的代码片段:

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

相关问题 更多 >

    热门问题