apachecn/apachecn-algo-zh

Undefined names: TreeNode, RandomListNode, and ListNode

cclauss opened this issue · 5 comments

flake8 testing of https://github.com/apachecn/awesome-algorithm on Python 3.7.1

$ flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

./docs/剑指offer/Python/61-序列化二叉树.py:27:20: F821 undefined name 'TreeNode'
            root = TreeNode(int(l[self.flag]))
                   ^
./docs/剑指offer/Python/25-复杂链表的复制.py:16:22: F821 undefined name 'RandomListNode'
            pClone = RandomListNode(pNode.label)
                     ^
./docs/剑指offer/Python/56-删除链表中重复的结点.py:11:17: F821 undefined name 'ListNode'
        first = ListNode(-1)
                ^
3     F821 undefined name 'RandomListNode'
3

E901,E999,F821,F822,F823 are the "showstopper" flake8 issues that can halt the runtime with a SyntaxError, NameError, etc. These 5 are different from most other flake8 issues which are merely "style violations" -- useful for readability but they do not effect runtime safety.

  • F821: undefined name name
  • F822: undefined name name in __all__
  • F823: local variable name referenced before assignment
  • E901: SyntaxError or IndentationError
  • E999: SyntaxError -- failed to compile a file into an Abstract Syntax Tree

@cclauss This code use python 2.7 for testing.

These undefined names remain the same in both Python 2 and Python 3.

The author just remain the definition of TreeNode commented, and this's kind of lc style.

You will see more in Leetcode_Solution/Python part.

The more Pythonic style might be:

class TreeNode:
    raise NotImplementedError('Please add your implementation here...')
    # def __init__(self, x):
    #   self.val = x
    #   self.left = None
    #   self.right = None

https://docs.python.org/3/library/exceptions.html#NotImplementedError