Python,__ init__和自我困惑 [英] Python, __init__ and self confusion

查看:86
本文介绍了Python,__ init__和自我困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以当我遇到这个问题时,我正在看一些资料来源:

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 file"
...         self.clear()
...         try:
...             fsock = open(filename, "rb", 0)
...             try:
...                 fsock.seek(-128, 2)
...                 tagdata = fsock.read(128)
...             finally:
...                 fsock.close()
...             if tagdata[:3] == 'TAG':
...                 for tag, (start, end, parseFunc) in self.tagDataMap.items():
...                     self[tag] = parseFunc(tagdata[start:end])
...         except IOError:
...             pass
... 

所以,我决定进行测试.

So, I decided to test it out.

 >>> __parse("blah.mp3")

而且,我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __parse() takes exactly 2 arguments (1 given)

这不是我第一次遇到这种情况,我一直认为我打算将self包含在参数参数列表中,但是我知道那是不对的.有人可以向我解释为什么我尝试使用的代码经常发生这种情况,我认为这是由于我对术语的理解水平,我什至什至不了解init或self所做的事情,或者它为什么相关. def x(b):print b与def x(self,b):self.b = b print self.b是不是?为什么这么重要!

This wouldn't be the first time I've encountered this, I keep thinking I'm meant to include self in the argument parameter list, but I know that that's not right. Could someone explain to me why this happens a lot with code I try to play around with, I suppose its due to my level of understanding about the terms, I barely even understand what init or self does, or why it's relevant. def x(b): print b is the same as def x(self, b): self.b = b print self.b isn't it? Why does it matter so much!

我只想要一个基本的解释,所以谢谢您.

I just want a basic explanation, so I can get this out of my mind,thanks.

推荐答案

def __parse在某些类定义中.

您不能将方法defs从类定义中拉出.方法函数定义是该类的一部分.

You can't pull the method defs out of the class definitions. The method function definition is part of the class.

看下面两个例子:

def add( a, b ):
    return a + b

还有

class Adder( object ):
    def __init__( self ):
        self.grand_total = 0
    def add( self, a, b ):
        self.grand_total += a+b
        return a+b

注释.

  1. 该函数不使用self.

class方法确实使用self.通常,所有实例方法都将使用self,除非它们具有诸如@classmethod这样的特定修饰符,否则它们将另行说明.

The class method does use self. Generally, all instance methods will use self, unless they have specific decorators like @classmethod that say otherwise.

该函数不依赖于其他任何东西.

The function doesn't depend on anything else else.

class方法取决于被类Adder的实例调用;此外,它取决于类Adder的实例是否已正确初始化.在这种情况下,初始化函数(__init__)确保Adder的每个实例始终都有一个名为grand_total的实例变量,并且该实例变量的初始值为0.

The class method depends on being called by an instance of the class Adder; further, it depends on that instance of the class Adder having been initialized correctly. In this case, the initialization function (__init__) has assured that each instance of Adder always has an instance variable named grand_total and that instance variable has an initial value of 0.

您不能将add方法函数从Adder类中拉出并单独使用.它不是独立功能.它是在类中定义的,并且由于在类中的定位而具有一定的期望.

You can't pull the add method function out of the Adder class and use it separately. It is not a stand-alone function. It was defined inside the class and has certain expectations because of that location inside the class.

这篇关于Python,__ init__和自我困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆