Python, __init__ and self confusion

前端 未结 5 536
别跟我提以往
别跟我提以往 2021-01-20 02:48

Alright, so I was taking a look at some source when I came across this:

>>> def __parse(self, filename):
...         \"parse ID3v1.0 tags from MP3 f         


        
5条回答
  •  温柔的废话
    2021-01-20 02:58

    Functions/methods can be written outside of a class and then used for a technique in Python called monkeypatching:

    class C(object):
      def __init__(self):
        self.foo = 'bar'
    
    def __output(self):
      print self.foo
    
    C.output = __output
    c = C()
    c.output()
    

提交回复
热议问题