[Tutor] "TypeError: 'int' object is not callable"??

Kent Johnson kent37 at tds.net
Sat Dec 18 00:21:11 CET 2004

Jacob S. wrote:
> Thank you!
> Wait, though.
> How do I do this?
> def differentnoofvars(*args,**kwargs):  ## By the way, is it **kwargs or
> **kwds?

Call it what you like, it's an ordinary function parameter. kwds is commonly used but you can use 
>     print kwargs
>     another(kwargs)

Should be another(**kwargs). If you call another(kwargs) then kwargs will be an ordinary parameter 
of another and another would be defined as
def another(kwargs):

> def another(**kwargs):
>     for x,y in kwagrs.items():
>         print "%s = %s" % (x,y)
> a = ['a=2','f=3','t=[1,2,3]']  ## A list of kwargs that I want to send

Should be a dict, the **kwds parameter is a dict mapping keywords to values
a = {'a':2, 'f':3, 't':[1,2,3]}

There really are two different and complementary things going on here, at the point of call and at 
the point of function definition.

At the point of call, you can pass a dictionary instead of using explicit, named parameters. For 
example, given a function test() defined like this:
  >>> def test(a, b):
  ...  print a, b

you can call it with ordinary named arguments:
  >>> test(a='foo', b='bar')
foo bar

Or you can pass it a dictionary with the named arguments, using extended calling syntax:
  >>> d= {'a':'foo', 'b':'bar'}
  >>> test(**d)
foo bar

Inside the function, if you have a **kwds parameter, it will receive a dict containing any keyword 
arguments not explicitly declared. This allows you to pass keyword parameters that you don't 
anticipate when the function is defined. For example,

  >>> def test2(a, **kwds):
  ...   print a
  ...   for k,v in kwds.items():
  ...     print k,v

  >>> test2(1)	# No keywords
  >>> test2(a=1)	# a is a declared parameter so kwds is empty
  >>> test2(1, b=2, c=3) # b and c are passed in kwds
c 3
b 2


> individually to differentnoofvars
> differentnoofvars(a)
>>>Hey, could you give an example?
>>>>apply() is deprecated; it has been replaced by 'extended
>>>call syntax'.
>>>Instead of
>>>>   apply(fn, args, kwds)
>>>>you can now write
>>>>   fn(*args, **kwds)
>>Here is a quick example I came up with:
>>>>>def spam(*args, **kwargs):
>>... print "Here are the args you supplied:"
>>... for item in args:
>>... print item
>>... print
>>... print "Here are the kwargs you supplied:"
>>... for key,value in kwargs.items():
>>... print key, '=', value
>>>>>spam(1,'a','eggs',s=0, p=1, a=2, m=3)
>>Here are the args you supplied:
>>Here are the kwargs you supplied:
>>a = 2
>>p = 1
>>s = 0
>>m = 3
>>In the case of the spam() function, 1, 'a', and 'eggs' are all put into
>>the sequence args (not sure if it is a list or tuple).  The key/value
>>pairs are bundled into the dictionary kwargs.  The arguments have to be
>>given in the right order though:
>>>>>spam(t=1, b=1, 'this', 'will', 'fail')
>>Traceback (SyntaxError: non-keyword arg after keyword arg
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list