zoukankan      html  css  js  c++  java
  • 错误记录(三):Python

    1,在函数中传入字典代替不定长参数

    func(**d)  # 传入时候要注意用**解包

    2,一些常见的名字少用,容易和系统或其他包重名

    3,递归中不能count+=1

    #!/usr/bin/python3
     
    a = 10
    def test():
        a = a + 1
        print(a)
    test()
    #以上程序执行,报错信息如下: Traceback (most recent call last): File "test.py", line 7, in <module> test() File "test.py", line 5, in test a = a + 1 UnboundLocalError: local variable 'a' referenced before assignment

    错误信息为局部作用域引用错误,因为 test 函数中的 a 使用的是局部,未定义,无法修改。

    将a用global声明或通过函数参数传递,可以正常执行输出结果,

    4,Python引用包很麻烦

    如果一个目录内有__init__.py,那就需要from 目录名.文件 import 函数 的方式引用

    而且同目录下需要用from . import xxx(此时__init__的文件名__name__是目录名)

    如果一个目录内没有__init__.py,那就可以直接import

    5,引用上级目录需要是添加__init__.py

    可以用from ..文件名 import 函数名的方式引用

    可以直接留言交流问题或想法,每天都会看
  • 相关阅读:
    257. Binary Tree Paths
    324. Wiggle Sort II
    315. Count of Smaller Numbers After Self
    350. Intersection of Two Arrays II
    295. Find Median from Data Stream
    289. Game of Life
    287. Find the Duplicate Number
    279. Perfect Squares
    384. Shuffle an Array
    E
  • 原文地址:https://www.cnblogs.com/shitianfang/p/12367348.html
Copyright © 2011-2022 走看看