Strange varargs issue

Chris cwitts at gmail.com
Fri Jan 4 08:58:35 EST 2008


On Jan 4, 3:45 pm, Mike <cki... at gmail.com> wrote:
> I'm not sure if this is a bug or if I'm just not understanding
> something correctly.  I'm running the following (broken.py) on
> ActivePython 2.5.1.1, based on Python 2.5.1 (r251:54863 5/1/2007) as
> "python broken.py foo" (on Windows, of course):
>
> #!/bin/env python
>
> import sys
>
> class foobar(object):
>     def func(arg):
>         print 'foobar.func: %r' % arg
>
> __f = foobar()
>
> def caller(a):
>     print 'caller: %r' % a
>     __f.func(a)
>
> def main():
>     rest = sys.argv[1:]
>     print 'main: %r' % rest
>     caller(*rest)
>
> if __name__ == '__main__':
>     main()
>
> ...and the result of running this ("python broken.py foo") is:
>
> main: ['foo']
> caller: 'foo'
> Traceback (most recent call last):
>   File "broken.py", line 21, in <module>
>     main()
>   File "broken.py", line 18, in main
>     caller(*rest)
>   File "broken.py", line 13, in caller
>     __f.func(a)
> TypeError: func() takes exactly 1 argument (2 given)
>
> How can this possibly be?  The "caller" print statement obviously
> shows "a" is singular.
>
> Thanks in advance for any and all insight...
>
> Mike

class foobar(object):
    def func(arg):
        print 'foobar.func: %r' % arg

def caller(a):
    __f.func()

>>> main: ['foo']
>>> caller: 'foo'
>>> foobar.func: <__main__.foobar object at 0x00A45550>

class foobar(object):
    def func(self, arg):
        print 'foobar.func: %r' % arg

def caller(a):
    __f.func(a)

>>> main: ['foo']
>>> caller: 'foo'
>>> foobar.func: 'foo'

You're already passing the object as an argument in the first case.



More information about the Python-list mailing list