簡體   English   中英

從python中的其他函數調用時,字典返回空

[英]Dictionary returned empty when called from other function in python

我正在嘗試從其他功能訪問字典(兩個功能都在同一類中)。 但這給我空字典。

class c1():
  def __init__(self):
       self.test = {}

  def func1(self,**dict1):
      pprint(dict1)       ## dict1 to this functions is passed from other python file and I am able to print this dict1 within in this function without any problem ##
      return dict1
  def func2(self):
     self.test = self.func1() # After adding changes suggested still returning empty dict

您在func1()中缺少return語句。

同樣,即使沒有打開字典,您也將調用不帶參數的func1()。

您可以嘗試以下方法:

def func2(self):
  self.test = self.func1(name='Pablo')

我也相信在您的__init__()test應該是self.test

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM