473,385 Members | 1,317 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,385 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 1083
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.