不了解此TypeError:“列表"对象不可调用 [英] Don't understand this TypeError: 'list' object is not callable

查看:104
本文介绍了不了解此TypeError:“列表"对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 im.py 的文件,其中包含几个函数和几个类.第一个函数和类失败

I have file called im.py which contains a couple of functions and several classes. The first function and class are failing with

TypeError: 'list' object is not callable

问题似乎是在函数中创建的列表的第一个元素,然后将其传递给类.该类可以处理大多数列表,但第一个元素不能.过渡到类名称空间似乎是一个问题:

The problem appears to be the first element of a list created in the function which is then passed to the class. Most of the list can be processed by the class, but the first element cannot. The transition to the class name space seems to be the problem:

def getdata_user():
    import os
    user = os.getlogin()
    gids = os.getgroups()
    home = os.getenv('HOME')
    return [user, gids, home]

class FirstClass:
    def setdata_user(self):
        self.user = getdata_user()    
    def displaydata_user(self):
        print(self.user)
    def user(self):
        u = self.user[0]
        return u
    def gids(self):
        g = self.user[1]
        return g
    def home(self):
        h = self.user[2]
        return h

在交互式会话中,所有功能都很好:

In the interactive session all is well in the function:

>>> from im import *
>>> a = getdata_user()
>>> print(a)
['fred', [4, 24, 27, 30, 46, 109, 125, 1000], '/home/fred']
>>> print(a[0])
jreese
>>> print(a[1])
[4, 24, 27, 30, 46, 109, 125, 1000]
>>> print(a[2])
/home/fred

但是在课堂上:

>>> b = FirstClass()
>>> b.setdata_user()
>>> print(b.home())
/home/jreese
>>> print(b.gids())
[4, 24, 27, 30, 46, 109, 125, 1000]
>>> print(b.user())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

所以回顾一下:

>>> type(b.user())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

>>> type(a[0])
<class 'str'>

因此很明显是什么问题.我只是不知道类转换如何掩盖列表的第一个元素.有什么想法吗?

So it's clear what is the problem. I just don't know how the class conversion is masking the first element of the list. Any ideas?

推荐答案

您不能对方法和属性使用相同的名称;方法只是特殊的属性.您的类可能具有 user()方法,但是通过在实例上设置属性 user ,Python会在那个而不是方法时找到那个您尝试访问 b.user :

You cannot use the same name for both a method and an attribute; methods are just special kinds of attributes. Your class may have a user() method, but by setting the attribute user on the instance, Python will find that instead of the method when you try to access b.user:

>>> type(b.user)  # no call, just the attribute
<class 'list'>

您必须重命名方法或属性,以免发生冲突.您可以在list属性中使用前导下划线,例如:

You'll have to rename either the method or the attribute to not conflict. You could use a leading underscore for the list attribute, for example:

class FirstClass:
    def setdata_user(self):
        self._user = getdata_user()    
    def displaydata_user(self):
        print(self._user)
    def user(self):
        u = self._user[0]
        return u
    def gids(self):
        g = self._user[1]
        return g
    def home(self):
        h = self._user[2]
        return h

这篇关于不了解此TypeError:“列表"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆