473,233 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 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 1076
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
2
by: Ian Partridge | last post by:
Hi, I want to write a varargs function which then passes its parameters to another varargs function. I have RTFFAQ: http://www.eskimo.com/~scs/C-faq/q15.12.html which says that the other...
9
by: grid | last post by:
Hi, I have a function which takes a variable number of arguments.It then calls va_start macro to initialize the argument list.But here in my case I have a special builtin va_start provided by the...
31
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char...
2
by: Rick Anderson | last post by:
I want to store a "va_list" in a structure (which gets passed around from function to function). I cannot use the va_copy() routine to create a copy of the varargs (it looks like it uses stack...
2
by: BinaryMonk | last post by:
Ok, this makes absolutely no sense to me, so I am hoping that someone here will have run across this issue as well. I have a pretty simple PHP script. which essentially just selects and displays...
13
by: Tim H | last post by:
Goal: to provide the simplest call-site possible at the cost of upfront hackery. I have a function that essentially takes a variable list of arguments: func(const string &k0, unsigned long...
3
by: Vish4u | last post by:
Hello Everyone, I have a encountered a strange issue with the execution of my stored procedure on clients machine. My stored procedure contains a cursor in which there is a select statement...
6
by: antianti | last post by:
Hello, I have a function that has a variable # of arguments of varying types. int foo(short a, ...) { va_list argp va_start(argp, a); ... va_end(argp);
2
by: scdowney | last post by:
First and foremost, thank you in advance for any attempts to help me out. I am working on a project with work, and it requires I use CSS selectors to locate elements within a webpage. For the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.