apachecn / apachecn-algo-zh

ApacheCN 数据结构与算法译文集
https://algo.apachecn.org/
11k stars 2.19k forks source link

Undefined names: TreeNode, RandomListNode, and ListNode #231

Closed cclauss closed 5 years ago

cclauss commented 5 years ago

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.

O2Dyokii commented 5 years ago

@cclauss This code use python 2.7 for testing.

cclauss commented 5 years ago

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

0xkookoo commented 5 years ago

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

0xkookoo commented 5 years ago

You will see more in Leetcode_Solution/Python part.

cclauss commented 5 years ago

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