Results 1 to 6 of 6

Thread: python: instance has no __call__ method

  1. #1
    Join Date
    Aug 2009
    Posts
    57

    python: instance has no __call__ method

    Hello friends,
    I recently started learning Python. I have written following code in Python. I the following code I have use 2 classes to define a few arrays of objects, which are used to store some unique strings and integers. The objects will consist of the following:
    Code:
    object[k].user
             .a     # a = k
             .names
             .coordss
             .heros
    After that I have written rest of the code. When I run this code I get following error message.
    AttributeError: CityBean instance has no __call__ method. I don't know what is that mean. pleas helm me to fix this problem.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: python: instance has no __call__ method

    From your information it is very difficult to understand what you want from your code. I think you have written wrong code and that's why you are getting such type of problem. In this case you have to write following code to fix this problem.
    Code:
    data = {
            (1, 'users1'): ("names1", "coords1", "heros1"),
            (2, 'users1'): ("names2", "coords2", "heros2"),
            #...
            (1, 'users2'): ("names11", "coords11", "heros11"),
            (2, 'users2'): ("names12", "coords12", "heros12"),
            # ...
        }
    
    
    class CityBean:
        def __init__(self,names,coordss,heros):
            self.names = names
            self.coordss = coordss
            self.heros = heros
    
    class Castles:
        def __init__(self,users,n):
            self.users = users
            self.n = n
            names, coordss, heros = data.get((n, users))
            self.citybean = CityBean(names, coordss, heros)

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: python: instance has no __call__ method

    You have written wrong code and that's why you are getting such type of problem. Why did your write following code?
    Code:
    temps = {
                CityBeans( "names21" , "coords21" , "heros21" ),
                
                CityBeans( "name30s" , "coord30s" , "heros30" ) }[self.n]()
    Have you think temp= {...}[something]() will do? If yes, then you are wrong. It only creates dictionary. You also have wrongly called CityBean object. This is function. You have create dictionaries only to pick a single item of it which is wrong.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: python: instance has no __call__ method

    You have put parenthesis after a class name and that's why you are getting such type of problem. When you put parenthesis after a class name, expression instantiates it immediately. If you are want to delay instantiation, then in this case you have to use lambda or functools.partial() to fix this problem. You can do this like:
    Code:
     lambda: CitysBeans("", "", "")}...

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: python: instance has no __call__ method

    I think you have written wrong code and that's why you are getting such type of problem. You have written following code:
    Code:
     CityBean( "names1"  , "coords1" , "heros1"),
    CityBean( "names10" , "coords10" , "heros10" )}[self.n]()
    In the above code you are tried to take value from dictionary. Values on your dictionary are CityBeans instances. In this case you have to write following:
    Code:
    CitysBeans( "names1"  , "coords1" , "heros1")()
    You also have to add a __call__ method depending on the need.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: python: instance has no __call__ method

    Look at your code in python:
    Code:
    CityBean( "names1"  , "coords1" , "heros1")
    When you write above code, you initialize the object CityBean. In above code there is no need for an extra (). You have also write make k\mistake in following code:
    Code:
    CityBean( "names10" , "coords10" , "heros10" )}[self.n]()
    In above code you are tried to call something which is not possible.

Similar Threads

  1. Method overriding versus method hiding in C#
    By ^MALARVIZHI^ in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 06:25 AM
  2. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:36 AM
  3. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  4. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM
  5. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,151,888.24122 seconds with 16 queries