我试图访问另一个函数声明的变量内声明的变量,但我得到了如何访问其他功能
ERROR:
AttributeError: 'Myclass1' object has no attribute 'myDictIn'
我使用的代码如下:
class Myclass1(object):
def __init__(self):
pass
def myadd(self):
x=self.myDictIn # tried accessing variable declared in another function
return x
def mydict(self): #variable declared in this function
myDictIn={1:[1,2,3,4],3:[4,5,6,7]}
self.myDictIn= myDictIn
return myDictIn
inst=Myclass1() # Created instance
inst.myadd() # Accessing function where I am using an variable declared in another function
而且我试图声明为全球
def mydict(self): #variable declared in this function
global myDictIn
myDictIn={1:[1,2,3,4],3:[4,5,6,7]}
self.myDictIn= myDictIn
return myDictIn
,但仍然得到了同样的错误
请帮我.... 其实我需要访问一个函数生成的变量和另一个函数中使用它....还有 我试过.....
- 声明为类 这些2种方法会导致进一步的错误
所以我必须能够访问一个函数内生成的varibale,并在另一个函数中使用它。请帮助我找到答案
你是什么意思“类型转换知道列表的大小”?你可以给一个*完整的例子*?为什么不只是不“转换为数组然后追加到列表”并使用数组? –
您似乎对'A [0]'感兴趣,而不是'A'。 “A”是如何定义的? – chepner
另外,Python中没有“typecast”运算符。虽然该术语的用法是“将此对象转换为另一种类型的对象”,但我不认为这种用法真的有用,它不像Java或C语言中的类型转换。 –