【问题标题】:How to Call function from another function [duplicate]如何从另一个函数调用函数[重复]
【发布时间】:2020-08-17 07:47:21
【问题描述】:

我试图在另一个函数中调用一个函数,但它返回一个错误。

class Data_Wrangling:
  def __init__(self):
    self.ClassName = type(self).__name__ + '.'
    self.Host = None
    self.App_Id = None
    self.App_Secret = None
    self.PySpace = None
  
  def Data_Retrieve(self,URL):
    
      print('\n<> ' + inspect.currentframe().f_code.co_name)
      
      JsonData = None
      ST = time.time()
      while JsonData is None:
          try:
              Response = requests.get(URL)
          except Exception as E:
              print('\n>> Failed: ' + str(E))
              if (time.time() - ST)/60 > 1:
                  print('\n>> Failed: NO valid JsonData Retrieved - Source Down or Internet Issue, etc')
                  break
              JsonData = None
              continue

          if Response.status_code == 200:
              try:
                  JsonData = json.loads(Response.content.decode('utf-8'))
              except Exception as E:
                  print('\n>> Failed: ' + str(E))
                  JsonData = None
                  continue
      KN.TPE(ST)
      return JsonData

  def Processing(self, DatasetId):
      Start_Date = '1900-01'
      today = datetime.today()
      End_Range = str(today.year)+ '-' + str(today.month)

      Limit = 5000
      Page = 1

      URL_Part1 = 'https://maps.clb.org.hk/map/pageList?keyword=&startDate='
      URL_Part2 = '&endDate='
      URL_Part3 = '&address=&industry=&parentIndustry=&industryName=&accidentType=&accidentTypeName=&companyOwnership=&companyOwnershipName=&deathsNumber=&deathsNumberName=&injuriesNumber=&injuriesNumberName=&mapType=2&page='
      URL_Part4 = '&limit='

      URL = URL_Part1 + Start_Date + URL_Part2 + End_Range + URL_Part3 + str(Page) + URL_Part4 + str(Limit)

      JsonData = Data_Retrieve(URL)

DW = Data_Wrangling()
Export_FilePath = DW.Processing('dtihwnb')

我收到错误:

    JsonData = Data_Retrieve(URL)
NameError: name 'Data_Retrieve' is not defined

我被困在这里,想着我在这里提出了什么问题。请帮助我在处理中调用 Data_Retrieve。

【问题讨论】:

    标签: python-3.x oop


    【解决方案1】:

    您需要使用 self 调用 Data_Retrieve 方法,因为它在类中。

    尝试像这样调用你的方法..

    JsonData = self.Data_Retrieve(URL)
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 1970-01-01
      • 2019-04-07
      • 2019-10-14
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多