Movatterモバイル変換


[0]ホーム

URL:


callbacks within classes

Adrian Grahamaag at stanford.edu
Mon Apr 2 18:22:57 EDT 2001


I'm in the process of writing a class based on a module that I havepreviously written. The problem that I'm running across is that since "self"is always the first argument to any function within a class, functions thatI have been using as callbacks no longer work since their signature haschanged to include self.For instance, what I used to code as this:def InsertDirectories():    os.path.walk(path, InsertDirectory, rs)def InsertDirectory(rs, dirname, names):    ...I'd like to code as this: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):        ...Unfortunately, I can't code it this way, since InsertDirectory's signatureis now: InsertDirectory(self, rs, dirname, names), which doesn't matchos.path.walk's callback signature. I know the easy (and inelegant) way tofix this problem is to simply move InsertDirectory outside of the class.This doesn't really make sense to me since the functionality really shouldbe contained within the class.I have the feeling that there is some elegant way to do what I want withPython, but that my C++/Java experience is getting in the way. Anysuggestions?Thanks,Adrian


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp