Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 自定义类问题:AttributeError:';xx和x27;对象没有属性';xx和x27;_Python_Class_Object_Recursion_Attributes - Fatal编程技术网

Python 自定义类问题:AttributeError:';xx和x27;对象没有属性';xx和x27;

Python 自定义类问题:AttributeError:';xx和x27;对象没有属性';xx和x27;,python,class,object,recursion,attributes,Python,Class,Object,Recursion,Attributes,我正在开发一个自定义类来解决这个问题,这是leetcode中非常常见的格式。问题在于: 最大和路径(硬)#在 给定二叉树。编写一个返回最大和的函数。A. 路径可以定义为任意两个节点之间的节点序列,并且 不一定要通过根 我的做法是: class Solution: def __inti__(self): self.maxSum = 0 def sumMax(self, root): # self.maxSum = 0 self.fi

我正在开发一个自定义类来解决这个问题,这是leetcode中非常常见的格式。问题在于:

最大和路径(硬)#在 给定二叉树。编写一个返回最大和的函数。A. 路径可以定义为任意两个节点之间的节点序列,并且 不一定要通过根

我的做法是:

class Solution:
    def __inti__(self):
        self.maxSum = 0

    def sumMax(self, root):
        # self.maxSum = 0
        self.findSum(root)
        return self.maxSum

    def findSum(self, root):
        if not root:
            return 0
        l = self.findSum(root.left)
        r = self.findSum(root.right)
        tempMax = l + r + root.value
        if tempMax > self.maxSum:
            self.maxSum = tempMax
        # self.maxSum = max(self.maxSum, tempMax)
        return max(l, r) + root.value

class TreeNode:
    def __init__(self, _value):
        self.value = _value
        self.left, self.right, self.next = None, None, None

def main():
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left.left = TreeNode(4)
    root.right.left = TreeNode(5)
    root.right.right = TreeNode(6)
    print(Solution().sumMax(root))

    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(3)
    root.right.left = TreeNode(5)
    root.right.right = TreeNode(6)
    root.right.left.left = TreeNode(7)
    root.right.left.right = TreeNode(8)
    root.right.right.left = TreeNode(9)
    print(Solution().sumMax(root))
main()
将返回错误消息,如下所示:

Traceback (most recent call last):
  File "/Users/tairanye/PycharmProjects/tester/main.py", line 46, in <module>
    main()
  File "/Users/tairanye/PycharmProjects/tester/main.py", line 33, in main
    print(Solution().sumMax(root))
  File "/Users/tairanye/PycharmProjects/tester/main.py", line 7, in sumMax
    self.findSum(root)
  File "/Users/tairanye/PycharmProjects/tester/main.py", line 13, in findSum
    l = self.findSum(root.left)
  File "/Users/tairanye/PycharmProjects/tester/main.py", line 13, in findSum
    l = self.findSum(root.left)
  File "/Users/tairanye/PycharmProjects/tester/main.py", line 16, in findSum
    if tempMax > self.maxSum:
AttributeError: 'Solution' object has no attribute 'maxSum'

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“/Users/tairanye/PycharmProjects/tester/main.py”,第46行,在
main()
文件“/Users/tairanye/PycharmProjects/tester/main.py”,第33行,在main中
打印(解决方案().sumMax(根))
sumMax中的第7行文件“/Users/tairanye/PycharmProjects/tester/main.py”
self.findSum(根)
文件“/Users/tairanye/PycharmProjects/tester/main.py”,第13行,findSum中
l=self.findSum(root.left)
文件“/Users/tairanye/PycharmProjects/tester/main.py”,第13行,findSum中
l=self.findSum(root.left)
文件“/Users/tairanye/PycharmProjects/tester/main.py”,第16行,findSum中
如果tempMax>self.maxSum:
AttributeError:“解决方案”对象没有属性“maxSum”
进程已完成,退出代码为1
我认为我遵循了正确的方式,但不确定为什么会发生这种错误。我可以通过在我的
sumMax
函数中插入
self.maxSum=0
来修复此错误,但我不确定以前的方法为什么不起作用。Python版本是
3.8

提前谢谢你的帮助


  • 更新:


打字错误造成了这个错误。但我还有一个问题。为什么Python在没有通知的情况下接受拼写错误的
\uuuu inti\uuu
。我不认为它默认使用这个命令。谢谢。

在第2行,您拼错了
def\uuuu init\uuuuuu(self):


这可能无法解决您的问题,但这是我发现的错误。

在第2行中,您拼错了
def\uuuu init\uuuuu(self):


这可能无法解决您的问题,但这是我发现的错误。

这应该可以解决问题,因为没有定义uu init uu,它从未运行,self.maxSum也从未初始化。感谢您的帮助。我完全没有注意到。我有一个关于
\uuuu inti\uuu
的后续问题。请随意分享你的意见。谢谢。这应该可以解决问题,因为没有定义_init __,它从未运行过,self.maxSum也从未初始化过。谢谢您的帮助。我完全没有注意到。我有一个关于
\uuuu inti\uuu
的后续问题。请随意分享你的意见。谢谢