471,326 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Strange varargs issue

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
Jan 4 '08 #1
4 1031
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
Jan 4 '08 #2
On Jan 4, 3:45 pm, Mike <cki...@gmail.comwrote:
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.
Jan 4 '08 #3
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-....html#id306617

</F>

Jan 4 '08 #4
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 ]
Jan 4 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Neil Ginsberg | last post: by
2 posts views Thread by Ian Partridge | last post: by
9 posts views Thread by grid | last post: by
31 posts views Thread by gamehack | last post: by
2 posts views Thread by Rick Anderson | last post: by
2 posts views Thread by BinaryMonk | last post: by
13 posts views Thread by Tim H | last post: by
6 posts views Thread by antianti | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.