im confused with __call__() method defined within the class. __init__() is used as constructor, and is called when we create an instance of the object.

when does __call__() method get called even though we not specify it explicitly...

please help..........

Recommended Answers

All 4 Replies

Hi!

__call__( self[, args...])
Called when the instance is ``called'' as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).

So ...

In [35]: class A:
   ....:     def __init__(self):
   ....:         print "init"
   ....:     def __call__(self):
   ....:         print "call"
   ....:

In [36]: a = A()
init

In [37]: a()
call

To be honest, I have never used this, and I don't really know where I could use it ;)

Regards, mawe

im confused with __call__() method defined within the class. __init__() is used as constructor, and is called when we create an instance of the object.

when does __call__() method get called even though we not specify it explicitly...

please help..........

There are several internal functions like __init__() and __call__() that a class calls on it's own. The __call__() function is simply used to call on a callable object like the callback() function outside the class. In Tkinter the command=callback statement can not pass function arguments, so instantiating class Curry will do it. Just a trick to get around the limitations of Tkinter.

class Curry: 
    """handles arguments for callback functions""" 
    def __init__(self, callback, *args, **kwargs): 
        self.callback = callback 
        self.args = args 
        self.kwargs = kwargs 

    def __call__(self): 
        #return apply(self.callback, self.args, self.kwargs)
        return self.callback(*self.args, **self.kwargs) # since Python23

Hope I didn't go too far over your head in my reply to your thread
http://www.daniweb.com/techtalkforums/thread38770.html

class Aclass:
	def __call__(self):
		print 'Hi I am __call__ed';
	
	def __init__(self, *args, **keyargs):
		print "Hi I am __init__ed";

Here, executing x = Aclass() will call __init__() and just x() will call __call__(). Hope this helps :)

Animals is always fun to make with oop design in mind.
Here you se __call___ used like this cow('gras')
This fill the the empty list(stomach)

class Animal(object):
    def __init__(self, name, legs):
        self.name = name
        self.legs = legs
        self.stomach = []        
        
    def __call__(self,food):
        self.stomach.append(food)
    
    def poop(self):
        if len(self.stomach) > 0:
            return self.stomach.pop(0)
        
    def __str__(self):        
        return 'A animal named %s' % (self.name)       
        
cow = Animal('king', 4)  #We make a cow
dog = Animal('flopp', 4) #We can make many animals
print 'We have 2 animales a cow name %s and dog named %s,both have %s legs' % (cow.name, dog.name, cow.legs)
print cow  #here __str__ metod work

#We give food to cow
cow('gras')
print cow.stomach

#We give food to dog
dog('bone')
dog('beef')
print dog.stomach

#What comes inn most come out
print cow.poop()
print cow.stomach  #Empty stomach

'''-->output
We have 2 animales a cow name king and dog named flopp,both have 4 legs
A animal named king
['gras']
['bone', 'beef']
gras
[]
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.