里個(gè),這些文檔是作為備忘錄筆記,不是為了展示,主要目標(biāo)讀者是某個(gè)患有嚴(yán)重健忘癥的程序猿,如果被你不小心讀到并迷惑不解,要么可以忽略,要么可以留言提問(wèn)。更希望能指出錯(cuò)誤。
Pycharm 環(huán)境設(shè)置
修改快捷鍵: File --> Settings --> Keymap : Visual Studio
PTVS 環(huán)境設(shè)置
標(biāo)識(shí)符顏色設(shè)置
function:166,226,46
param:253,151,31
key:102,217,239
operator:249,38,114
Visual Assit 環(huán)境設(shè)置
允許VC識(shí)別其他擴(kuò)展,這里是識(shí)別成C++文件了。。。
Allow C/C++ files with a non-standard extension
其中Add your extension to:"Tools | Options | Text Editor | File Extension | Microsoft Visual C++"不要修改了,否則會(huì)將py文件當(dāng)成C++,所有#注釋都被VA識(shí)別成了宏定義,很悲劇...
Support for python (work with pytools)
扯了些淡,2012年的,也折騰了下,不知道到底其作用了沒(méi)(上一個(gè)一起設(shè)置了,所以不好區(qū)分)
Outline不能用,Open File in Solution可以用,還行
參數(shù)組(元組*、字典**)
將參數(shù)放到一個(gè)元組或字典中,func(params[,params2], *tuple_grp_nonkw_args, **dict_grp_kw_args)
形參、元組、字典的順序不能變
三者可以缺少其中某2個(gè)或兩個(gè)
調(diào)用時(shí)傳的參數(shù)也必須對(duì)應(yīng)順序
fun = lambda *z : z 同樣適用
代碼組
縮減相同的一組語(yǔ)句構(gòu)成代碼組。如if、while、def、class等符合語(yǔ)句以關(guān)鍵字開始,以冒號(hào)':'結(jié)束,之后接代碼組。
代碼組可以稱為子句(clause)
修飾器(decorate)
def f_decorate(func):
print 'begin f_decorate'
def func_wrapper(name):
print '<func_wrapper>'
return "<p>{0}</p>".format(func(name))
print 'end f_decorate'
return func_wrapper
@f_decorate
def g_function(name):
print '<g_function>'
return "lorem ipsum, {0} dolor sit amet".format(name)
ret = g_function("John")
預(yù)處理期
先組裝新函數(shù)對(duì)象
#g_function = f_decorate(g_function)
其實(shí)執(zhí)行的函數(shù)對(duì)象是f_decorate,g_function只是參數(shù)
新編譯的函數(shù)暫且叫g(shù)_function@
執(zhí)行期
按部就班調(diào)用新函數(shù)g_function@,記得存在原始函數(shù)對(duì)象g_function在新的函數(shù)對(duì)象環(huán)境里;
而在修飾器函數(shù)體里,func_wrapper代表了新函數(shù)功能,func(name)代表了原來(lái)的函數(shù)功能,
查看
globals和locals均可查看到新生成的函數(shù)對(duì)象
#print globals()
#print locals()
運(yùn)行時(shí)行號(hào)、文件名等
(frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[0]
<占位項(xiàng)1>
<占位項(xiàng)2>
Pip
Tool for installing Python packages
https://pypi.python.org/pypi/pip
C:\Python27\Scripts 加入環(huán)境變量,pip.exe在這里
<占位項(xiàng)2>
SimpleXMLRPCServer— Basic XML-RPC server
https://docs.python.org/2.6/library/simplexmlrpcserver.html
Server
from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(('localhost', 9000))
def my_function(a, b):
return a * b
server.register_function(my_function, 'multiply args')
try:
print 'Use Control-C to exit'
server.serve_forever()
except KeyboardInterrupt:
print 'Exiting'
Client
import xmlrpclib
proxy = xmlrpclib.ServerProxy('http://localhost:9000')
print getattr(proxy, 'multiply args')(5, 5)
<占位標(biāo)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項(xiàng)1>
<占位項(xiàng)2>
扯個(gè)全局對(duì)象的蛋
這個(gè)蛋有點(diǎn)扯,在當(dāng)前作用域,你可以使用全局變量,但是這個(gè)時(shí)候如果你用等號(hào)賦值這個(gè)名字,這個(gè)時(shí)候這個(gè)變量其實(shí)是局部重新聲明的一個(gè)變量,而且不管這句是在塊的任何位置。
Instance=None
print '1 global instance id:',id(Instance)
class BossAIComp():
def __init__(self):
#Instance = self
print '2 instance id:',id(Instance)
'#Instance = self'這句如果被注釋了,你使用的就是全局的Instance
變量名字在當(dāng)前域任何地方只要賦值,這個(gè)塊里這個(gè)名字都是局部的。例如"Instance = self"如果放在“print '2”下面,也會(huì)直接將這個(gè)變量當(dāng)成局部的,同時(shí)在使用的時(shí)候提示這個(gè)變量沒(méi)有定義
再扯個(gè)import的蛋
用法的不同形式的蛋
################################################
# case 1
import xxx.yyy.BossAIComp
xxx.yyy.BossAIComp.Instance.say()
# case 2
import xxx.yyy.BossAIComp as ai
ai.Instance.say()
# case 3
from xxx.yyy.BossAIComp import Instance
Instance.say()
# case 4
from xxx.yyy.BossAIComp import Instance as ist
ist.say()
<占位標(biāo)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項(xiàng)1>
<占位項(xiàng)2>
<占位標(biāo)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項(xiàng)1>
<占位項(xiàng)2>
<占位標(biāo)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項(xiàng)1>
<占位項(xiàng)2>
修改記錄
2014-07-15 創(chuàng)建
---