[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Strange varargs issue

Mike

1/4/2008 1:45:00 PM

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
4 Answers

Diez B. Roggisch

1/4/2008 1:51:00 PM

0

Mike schrieb:
> 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

This needs to be

def func(self, arg):
....


And then of course for aclling, you need an instance of foobar as first
argument. Either explicit, or implicit:

unbound_m = foobar.func
unbound_m(some_foobar, arg)

bound_m = foobar().func
bound_m(arg)

Or you do

@classmethod
def func(cls, arg):
...


Then you only need one argument, but beware: it's a classmethod, not an
instancemethod anymore.

Diez
Diez

Chris

1/4/2008 1:59:00 PM

0

On Jan 4, 3:45 pm, Mike <cki...@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.

Fredrik Lundh

1/4/2008 2:01:00 PM

0

Mike wrote:

> __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.

__f.func(a) is a method call, and methods always get the object itself
as an extra initial argument. to fix this, add "self" to the method
signature:

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

see the tutorial for more info.

> I'm not sure if this is a bug or if I'm just not understanding
> something correctly.

you are aware that blaming your mistakes on bugs in widely used code is
somewhat rude, right?

http://www.catb.org/~esr/faqs/smart-questions.htm...

</F>

Mike

1/4/2008 2:13:00 PM

0

You know, every once in a while, self really bites me. (I program in
Java too much)

Thanks for everyone who replied quickly.

Mike wrote:
>> [ a bunch of crap because I forgot self, nevermind sorry ]