callbacks within classes

Penfold spam at spam.com
Mon Apr 2 18:48:40 EDT 2001


"Adrian Graham" <aag at stanford.edu> wrote in message
news:9aau45$3sf$1 at nntp.Stanford.EDU...
> I'm in the process of writing a class based on a module that I have
> previously written. The problem that I'm running across is that since
"self"
> is always the first argument to any function within a class, functions
that
> I have been using as callbacks no longer work since their signature has
> changed to include self.
> class FileManager:
>
>     def __init__(self):
>         self.path = << some path >>
>         self.rs = << a record set >>
>
>     def InsertDirectories(self):
>         os.path.walk(self.path, self.InsertDirectory, self.rs)
>
>     def InsertDirectory(rs, dirname, names):
>         ...
try making that def insertDirectory(self, rs, dirname, names): ...
not that you actually need to pass rs, as you can get to it through self.

> Unfortunately, I can't code it this way, since InsertDirectory's signature
> is now: InsertDirectory(self, rs, dirname, names), which doesn't match
> os.path.walk's callback signature. I know the easy (and inelegant) way to

try it, you may be surprised ... since you pass self.insertDirectory to
walk,
the self parameter has already been bound ... making its signature exactly
what you want.

> I have the feeling that there is some elegant way to do what I want with
> Python, but that my C++/Java experience is getting in the way. Any
> suggestions?







More information about the Python-list mailing list