Python基礎--1

1)數據交換:python

C語言中:temp=x;函數

             x=y;this

     y=tempspa

Python中:x,y=y,xdebug

2) 典型的Python文件結構code

 1 #/usr/bin/env python             #(1)起始行
 2 
 3 「this is a test module」          #(2)模塊文檔
 4 
 5 import sys                       #(3)模塊導入
 6 import os
 7 
 8 debug=True                       #(4)(全局)變量定義
 9 
10 class FooClass(object):          #(5)類定義(如有)
11     "Foo class"
12     pass
13 
14 def test():                      #(6)函數定義(如有)
15     "test function"
16     foo=FooClass()
17     if debug:
18         print('ran test()')
19 
20 if(__name__=='__main__'):      #(7)主程序
21     test()

 3) 內存管理:對象

a.變量無需事先聲明;變量在第一次被賦值時自動聲明blog

b.變量無需指定類型內存

c.咱們不用關心內存管理資源

d.變量名會被’回收‘的

e.del語句能直接釋放資源

4)增長引用計數有:

a.對象被建立:x=3.14

b.另外的別名被建立:y=x

c.做爲參數傳遞給函數(新的本地引用):foobar(x)

d.成爲容器對象的一個元素:mylist=[123,x,'xyz']

5)減小引用計數:

a.一個本地引用離開了其做用範圍;好比foobar(x)函數結束時:

b.del y  

c.對象的一個別名被賦值給其它對象:

1 x=3.14
2 y=x
3 x=123

d.對象被從一個窗口對象中移除:mylist.remove(x)

e.窗口對象自己被銷燬:del mylist

相關文章
相關標籤/搜索