里個,這些文檔是作為備忘錄筆記,不是為了展示,主要目標(biāo)讀者是某個患有嚴(yán)重健忘癥的程序猿,如果被你不小心讀到并迷惑不解,要么可以忽略,要么可以留言提問。更希望能指出錯誤。
Pycharm 環(huán)境設(shè)置
修改快捷鍵: File --> Settings --> Keymap : Visual Studio
PTVS 環(huán)境設(shè)置
標(biāo)識符顏色設(shè)置
function:166,226,46
param:253,151,31
key:102,217,239
operator:249,38,114
Visual Assit 環(huán)境設(shè)置
允許VC識別其他擴展,這里是識別成C++文件了。。。
Allow C/C++ files with a non-standard extension
其中Add your extension to:"Tools | Options | Text Editor | File Extension | Microsoft Visual C++"不要修改了,否則會將py文件當(dāng)成C++,所有#注釋都被VA識別成了宏定義,很悲劇...
Support for python (work with pytools)
扯了些淡,2012年的,也折騰了下,不知道到底其作用了沒(上一個一起設(shè)置了,所以不好區(qū)分)
Outline不能用,Open File in Solution可以用,還行
參數(shù)組(元組*、字典**)
將參數(shù)放到一個元組或字典中,func(params[,params2], *tuple_grp_nonkw_args, **dict_grp_kw_args)
形參、元組、字典的順序不能變
三者可以缺少其中某2個或兩個
調(diào)用時傳的參數(shù)也必須對應(yīng)順序
fun = lambda *z : z 同樣適用
代碼組
縮減相同的一組語句構(gòu)成代碼組。如if、while、def、class等符合語句以關(guān)鍵字開始,以冒號':'結(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ù)對象
#g_function = f_decorate(g_function)
其實執(zhí)行的函數(shù)對象是f_decorate,g_function只是參數(shù)
新編譯的函數(shù)暫且叫g(shù)_function@
執(zhí)行期
按部就班調(diào)用新函數(shù)g_function@,記得存在原始函數(shù)對象g_function在新的函數(shù)對象環(huán)境里;
而在修飾器函數(shù)體里,func_wrapper代表了新函數(shù)功能,func(name)代表了原來的函數(shù)功能,
查看
globals和locals均可查看到新生成的函數(shù)對象
#print globals()
#print locals()
運行時行號、文件名等
(frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[0]
<占位項1>
<占位項2>
Pip
Tool for installing Python packages
https://pypi.python.org/pypi/pip
C:\Python27\Scripts 加入環(huán)境變量,pip.exe在這里
<占位項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)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項1>
<占位項2>
扯個全局對象的蛋
這個蛋有點扯,在當(dāng)前作用域,你可以使用全局變量,但是這個時候如果你用等號賦值這個名字,這個時候這個變量其實是局部重新聲明的一個變量,而且不管這句是在塊的任何位置。
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)前域任何地方只要賦值,這個塊里這個名字都是局部的。例如"Instance = self"如果放在“print '2”下面,也會直接將這個變量當(dāng)成局部的,同時在使用的時候提示這個變量沒有定義
再扯個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)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項1>
<占位項2>
<占位標(biāo)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項1>
<占位項2>
<占位標(biāo)題>
<占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述占位描述>
<占位項1>
<占位項2>
修改記錄
2014-07-15 創(chuàng)建
---