【发布时间】:2015-10-31 20:34:10
【问题描述】:
下面的代码返回错误:
class my_class:
def __init__(self):
self.l = [(1,2),(3,4)]
def funct(self):
self.l = list(map(lambda x: x[0], l))
print (l)
ob = my_class()
ob.funct()
TypeError: 'int' 对象不可下标
但是,如果要将逻辑与类分开 - 它会按预期工作
l = [(1,2),(3,4)]
l = list(map(lambda x: x[0], l))
print (l)
[1, 3]
谁能解释一下为什么对类变量执行相同的操作会导致上述错误?
附:我正在使用 python 3.4,虽然不认为这很重要
【问题讨论】:
-
这是我的问题,我把实际代码搞砸了,这个问题对社区没有帮助。请删除它。谢谢
标签: python python-2.7 python-3.x ipython ipython-notebook