newbie class troubles

Calvelo Daniel dcalvelo at pharion.univ-lille2.fr
Mon Sep 18 12:01:29 EDT 2000


[Brett Lempereur]
: I wasn't actually given a line number!!  The python interpreter literally
: just threw the error right at me, without a point of reference, which is why
: it is so difficult to actually track down the error 

[Jeremy Hylton <jeremy at beopen.com>]
: That's a surprise.  I just tried to generate an error with the code
: you posted earlier in this thread.  I wrapped the filesearch class up
: in a module, then added a little code that passed it to os.path.walk
: incorrectly.  I got an error:

: bitdiddle:~> python /tmp/foo.py
: Traceback (most recent call last):
:   File "/tmp/foo.py", line 23, in ?
:     os.path.walk("/", filesearch.search, None)
:   File "/usr/local/lib/python2.0/posixpath.py", line 265, in walk
:     func(arg, top, names)
: TypeError: unbound method must be called with class instance 1st argument

: It tells me just what went wrong -- the variable func is bound to an
: unbound method but the variable arg is not an instance of that class.
: The offending line in my code is line 23 of the foo.py module.

[Jeremy explains the "cut'n'paste" your error thing, then]

: But that doesn't work either.  I get a different error message:

: bitdiddle:~> python /tmp/foo.py
: Traceback (most recent call last):
:   File "/tmp/foo.py", line 23, in ?
:     os.path.walk("/", filesearch().search, None)
:   File "/usr/local/lib/python2.0/posixpath.py", line 265, in walk
:     func(arg, top, names)
: TypeError: too many arguments; expected 2, got 4

: It looks like the search method in class filesearch is only prepared
: to take two arguments (self and top) but is being called with four.
: That's your code, so I can't fix it for you.

Brett, from what I gathered from your code, you are actually reproducing 
more or less os.path.walk's functionality. Your function/method (at least
when patched as in a previous message) fills the 'filelist' variable
recursively with a complete path of all files starting at 'top' and
excluding those of 'excludelist'.

Now,

>>> print os.path.walk.__doc__
walk(top,func,args) calls func(arg, d, files) for each directory "d" 
in the tree  rooted at "top" (including "top" itself).  "files" is a list
of all the files and subdirs in directory "d".

os.path.walk builds that list itself, *then* calls the function you
give to it, with the 'args' argument plus the 'd'irectory and 'files'
into that directory. You could then use your 'search' method on each
directory, but I don't see much point in doing so.

Sorry for being dense on the "how to post" discourse, but had you stated
the purpose of your code, we might had much more to say.

who-reads-these-posts-anyway?-ly y'rs

Daniel.

-- Daniel Calvelo Aros
     calvelo at lifl.fr



More information about the Python-list mailing list