Calling a class function inside of __init__

前端 未结 6 1841
夕颜
夕颜 2020-12-02 06:13

I\'m writing some code that takes a filename, opens the file, and parses out some data. I\'d like to do this in a class. The following code works:

class MyCl         


        
6条回答
  •  -上瘾入骨i
    2020-12-02 06:37

    Call the function in this way:

    self.parse_file()
    

    You also need to define your parse_file() function like this:

    def parse_file(self):
    

    The parse_file method has to be bound to an object upon calling it (because it's not a static method). This is done by calling the function on an instance of the object, in your case the instance is self.

提交回复
热议问题