02Python学习笔记之二.一【import、==和is、深浅

2019-08-17  本文已影响0人  平知
章节号 内容            
1图片格式(png) 宽度大于620px,保持高宽

第1章节  import模块

  ↓import导入模块的搜索路径:

In [1]: import sys

In [2]: sys.path
Out[2]: 
#第一个为当前路径
['',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/li/.local/lib/python3.6/site-packages',
 '/usr/local/lib/python3.6/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/usr/lib/python3/dist-packages/IPython/extensions',
 '/home/li/.ipython']

In [3]: 

  ↓添加一条搜索路径:

In [4]: sys.path.append("/home")

In [5]: sys.path
Out[5]: 
['',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/li/.local/lib/python3.6/site-packages',
 '/usr/local/lib/python3.6/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/usr/lib/python3/dist-packages/IPython/extensions',
 '/home/li/.ipython',
 '/home']

In [6]: 

  ↓sys.path的一些用法:

  1、自定义模块并导入:
  ↓在某路径下,新建一个test.py文件,输入以下代码:

vim test.py
  1 def test():
  2     print("test import")

  ↓在sys.path中添加当前路径,便可导入使用:

In [1]: import sys

In [2]: sys.path
Out[2]: 
['',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/IPython/extensions',
 '/home/li/.ipython']

In [3]: sys.path.append("/home/li")

In [4]: sys.path
Out[4]: 
['',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/IPython/extensions',
 '/home/li/.ipython',
 '/home/li']

In [5]: import test

In [6]: test.test()
test import

In [7]: 

  ↑退出后path的添加会失效,再次使用需要再次导入。

  2、重新导入模块reload(模块)
  应用场景:在导入一个模块后,该模块代码被修改,如果不重新导入,则新修改的代码无法使用。再次使用import也不能完成预期,正确做法为

In [7]: reload(test)
Out[7]: <module 'test' from 'test.pyc'>

  什么是循环导入?
  我用你,我导入你。同时你也用我,你导入我。

from 模块名 import 函数名
root@li-ThinkPad-T420s:/home/li# cat test.py
from text import text
def test():
    print("test import")
    text()

test()


root@li-ThinkPad-T420s:/home/li# cat text.py
from test import test

def text():
    print("text import")
    test()

text()

root@li-ThinkPad-T420s:/home/li# 
root@li-ThinkPad-T420s:/home/li# python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from text import text
  File "/home/li/text.py", line 1, in <module>
    from test import test
  File "/home/li/test.py", line 1, in <module>
    from text import text
ImportError: cannot import name text
root@li-ThinkPad-T420s:/home/li# 

  ↑从设计角度来说,这种情况要严格避免。

第2章节  == 和 is

  ==是判断是否相同
  is是判断id是否相同(是否指向同一个东西)

In [20]: a=[1,2]

In [21]: b=[1,2]

In [22]: a is b
Out[22]: False

In [23]: id(a)
Out[23]: 140065919094080

In [24]: id(b)
Out[24]: 140065919170680

In [25]: a == b
Out[25]: True

  ↓注意这个问题,数字在负的X到正的X这一范围内,所有的id会变成相同的,超过这个范围则会不同。切记。

In [26]: a = 100

In [27]: b = 100

In [28]: a == b
Out[28]: True

In [29]: a is b
Out[29]: True
In [30]: a=10000

In [31]: b=10000

In [32]: id(a)
Out[32]: 94786674642736

In [33]: id(b)
Out[33]: 94786674642640

第3章节  深拷贝和浅拷贝

  1、深:我拿到了数据(copy.deepcopy)。改一个,另一个不变。

In [35]: a=[1,2,3]

In [39]: import copy

In [40]: b=copy.deepcopy(a)

In [41]: id(a)
Out[41]: 140065900535752

In [42]: id(b)
Out[42]: 140065909328424
#深拷贝

  2、浅:我只拿到了地址。改一个,另一个也跟着变。

In [35]: a=[1,2,3]

In [36]: b=a

In [37]: id(a)
Out[37]: 140065900535752

In [38]: id(b)
Out[38]: 140065900535752
#浅拷贝

  深拷贝对多重引用会自动进行递归拷贝,一直到最底层。

In [55]: a
Out[55]: [1, 2, 3, 4]

In [56]: b
Out[56]: [1, 2, 3]

In [57]: c=[a,b]

In [58]: c
Out[58]: [[1, 2, 3, 4], [1, 2, 3]]

In [59]: d=c

In [60]: id(c)
Out[60]: 140065909614568

In [61]: id(d)
Out[61]: 140065909614568

In [62]: e=copy.deepcopy(c)

In [63]: id(e)
Out[63]: 140065909636632

In [64]: id(e[0])
Out[64]: 140065919104784

In [65]: id(c[0])
Out[65]: 140065900535752

In [66]: id(d[0])
Out[66]: 140065900535752

In [67]: e
Out[67]: [[1, 2, 3, 4], [1, 2, 3]]

  copy模块中,还有一个copy.copy函数。

In [70]: a=[1,2]

In [71]: b=[3,4]

In [72]: c=[a,b]

In [73]: c
Out[73]: [[1, 2], [3, 4]]

In [74]: d=copy.copy(c)

In [75]: d
Out[75]: [[1, 2], [3, 4]]

In [76]: id(c)
Out[76]: 140065919152424

In [77]: id(d)
Out[77]: 140065918710200

In [78]: id(c[0])
Out[78]: 140065919059368

In [79]: id(d[0])
Out[79]: 140065919059368

  copy()只识别第一层引用。
  deepcopy()识别所有引用,一直到最底。

In [81]: a
Out[81]: [1, 2]

In [82]: b
Out[82]: [3, 4]

In [83]: c=(a,b)

In [84]: d=copy.copy(c)

In [85]: id(c)
Out[85]: 140065900473808

In [86]: id(d)
Out[86]: 140065900473808

In [87]: id(c[0])
Out[87]: 140065919059368

In [88]: id(d[0])
Out[88]: 140065919059368

In [89]: id(c[0][0])
Out[89]: 94786668758568

In [90]: id(d[0][0])
Out[90]: 94786668758568

  元组本身是不可变类型,copy()判断出是不可变类型,第一层引用都不深入拷贝。copy()判断出是可变类型,深入拷贝第一层。

  copy模块的功能一定要慎用,想清楚再用!

上一篇下一篇

猜你喜欢

热点阅读