Don't understand this TypeError: 'list' object is not callable

前端 未结 2 647
轻奢々
轻奢々 2021-01-27 15:47

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\'         


        
2条回答
  •  深忆病人
    2021-01-27 16:28

    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
    
    

    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
    

提交回复
热议问题