Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python同时调用这两个函数?_Python - Fatal编程技术网

为什么Python同时调用这两个函数?

为什么Python同时调用这两个函数?,python,Python,我有一些问题要理解这里发生了什么 这是我的消息来源: class Calc(): def __init__(self,Ideal,Limit,Value,Debug=None): self.Ideal = Ideal self.Limit = Limit self.Value = Value self.Debug = Debug self.Grade = self.GetGrade() s

我有一些问题要理解这里发生了什么

这是我的消息来源:

class Calc():

    def __init__(self,Ideal,Limit,Value,Debug=None):

        self.Ideal = Ideal
        self.Limit = Limit
        self.Value = Value
        self.Debug = Debug
        self.Grade = self.GetGrade()
        self.LenGrade = self.GetLenGrade()

    def GetGrade(self):
        if self.Debug:
            print('calling GetGrade')
        return Grade

    def GetLenGrade(self):
        if self.Debug:
            print('calling GetLenGrade')
        return Grade
当你打电话给我的时候

GradeMinLen += Calc(TargetLen, LimitMinLen, Length ,Debug=1).LenGrade
我总是得到输出

calling GetGrade    
calling GetLenGrade
为什么python要调用GetGrade?

您创建了一个Calc实例,每当您这样做时,就会为该新实例调用Calc.\uu init\uuuuuuuuuuu

您的Calc.\uuuu init\uuuuuu方法同时调用self.GetGrade和self.GetLenGrade:


在这里,创建实例后只访问LenGrade属性并不重要;__init__中的上述两行不存储方法引用,它们存储方法调用的结果。然后,Calc…LenGrade返回其中一个结果;但是,还有另一个结果。

在对象初始化代码中,您有以下内容:

self.Grade = self.GetGrade()
self.LenGrade = self.GetLenGrade()
这意味着将数据成员等级的值设置为通过调用方法GetGrade获得的值,对于LenGrade也是如此


调用它们并不奇怪,如果不调用,那就更令人惊讶了。

您在uu init_uu中调用了这两个函数。
self.Grade = self.GetGrade()
self.LenGrade = self.GetLenGrade()