newbie class troubles

Jeremy Hylton jeremy at beopen.com
Mon Sep 18 11:08:17 EDT 2000


"Brett Lempereur" <a.lempereur at cableinet.co.uk> writes:

> > "In any programming language newsgroup, the proper way to get help is to
> <*snip*>"
> 
> Allright.  i do know how to post programming error questions into newsgroups
> (Vb and C++) but in python it is obviously different.  In case you're
> interested i'm doing the testing in the immediate view

Python is different from Vb and C++.  It may take a little getting
used to, but error reporting and debugging are pretty easy.  The error
messages in Python 2.0 are generally much better than in earlier
versions. 

I'm not sure what you mean by the immediate view.  Perhaps the
interactive interpreter prompt?  I'm not sure exactly, but you seem to
use a different set of terminology than most people on
comp.lang.python.  That's neither good nor bad a priori, but since you
use differents words it would help if you used more of them.

> > "pointing that line out in your code, so we don't have to count"
> 
> 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

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.

Let's take a look at it:

class filesearch:
    # Any public variables i need
    exceptions = []
    filelist = []
    dirlist = []

    def search(self, top):
        try:
            tmpList = os.listdir(top)
        except os.error:
            return

        for item in tmpList:
            if item not in exceptions:
                name = os.path.join(top, name)
                if os.path.isdir(name):
                    self.dirlist.append(name)
                    search(self, name)
                else:
                    self.filelist.append(name)

import os
os.path.walk("/", filesearch.search, None)  # line 23

Looks like I should have tried

import os
os.path.walk("/", filesearch().search, None)  # line 23

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.

-- Jeremy Hylton <http://www.python.org/~jeremy/>



More information about the Python-list mailing list