跳至主要内容

Python学习笔记20100117

映射list
 
Dictionary 是用{}. list 是一那个[]. turple是用()
当你定义过dictionary后,你可以使用d.keys(), d.values(),d.items()将定义后的时候分别显示出来
当然可以将list里的值加减乘除,也可以如一般的定义直接重新复制这个list.
链接与分割字符串的
li=";", join(li) 则显示的就是 分隔符为;的数 如果使用li=li.split(";")则将刚刚;的分隔符删除,而split里也可定义域,如li.split(";",1)
 
自省 <---这是啥
之前在使用的时候发觉有些书本上的模块不能调用,很多是py脚本定义过的函数。
下载该脚本s,然后上传至指定位置。
>>>import sys
>>>sys.path
>>>sys.path.append("绝对位置")然后就能调用这些脚本和参数了。
删除
>>>sys.path.pop()
定义一个参数是 def info(test, test1=10.test2=12):
info是函数名,test是必备参数,因为没有定义值,test1和test2是可选参数,定义了初始值
 
以上是外部函数的调用,下面转到内部函数
内部函数有type,str,dir及其他
 
---type
返回任意字符的类型,模块也可以。types模块
>>> type(1)
<type 'int'>
>>> li=[]
>>> type(li)
<type 'list'>
>>> import odbchelper
>>> type(odbchelper)
<type 'module'>
>>> type(sys)
<type 'module'>
>>> import types
>>> type(odbchelper) == types.ModuleType
True
 
 
---str
str将强制转换字符串
>>> str(horsemen)
"['war', 'pestilence', 'famine', 'testing']" <---这个是一个字符串""
>>> str(odbchelper)
"<module 'odbchelper' from '/home/xtuulei/py/odbchelper.py'>"
str 还允许作用于模块。注意模块的字符串形式表示包含了模块在磁盘上的路径名,所以你的显示结果将会有所不同。
>>> str(None) ---书上说这个很重要,对空值的使用
'None'
 
---dir
相比较而言,不是很熟这个,但是貌似可以列出来所有li,d,funcation所包含的key
--callable
 
type、str、dir 和其它的 Python 内置函数都归组到了 __builtin__
你可以在其中使用
>>>form apihelper import info
>>>import __builtin__
>>>info(__buitin__)
 
---getattr
>>getattr(li,"pop")
该语句也是返回 pop 方法的引用,但是此时,方法名称是作为一个字符串参数传递给 getattr 函数的。getattr 是一个有用到令人无法致信的内置函数,可以返回任何对象的任何属性。
>>>getattr(li,"append")("Moe")
>>>li
getattr 的返回值 是 方法,然后你就可以调用它就像直接使用 li.append("Moe") 一样。但是实际上你没有直接调用函数;只是以字符串形式指定了函数名称。
getattr 也可以作用于字典
getattr 不仅仅适用于内置数据类型,也可作用于模块
 
getattr也可以做为一个分发者
getattr作为一个分发者的实例
statsout是附带的脚本,里面已经定义了output_html,output_xml,output_text
 
import statsout
def output(data, format="text"):
    output_function = getattr(statsout, "output_%s" % format, statsout.output_text)
    return output_function(data)
output_function第三个参数是被定义为缺省参数,在调用 getattr 时添加了第三个参数。第三个参数是一个缺省返回值,如果第二个参数指定的属性或者方法没能找到,则将返回这个缺省返回值
 

--
Alex Tu
-----------------------------------------------------------
ShangHai,China

评论

此博客中的热门博文

Setagaya park

  We visited Setagaya Park (世田谷区) yesterday. This park reminds us when we in Europe, which has a fancy fountain in the middle, and free entrance. People just laid down on the grass, enjoy the day time they can waste. people who have kids together,  play all different kinds of sports. you really can't image whether you are in Europe or  Japan. I would prefer to use a Bus instead of the metro, the metro is fast, but you only can see the dark, light, people sit or stand as Sardine, without any emotion. but on the bus, you can see the traffic, the shop, everything on the road. When we on our way to the park, we stop by a good chocolate shop named " craft chocolate works ",  this is a very good place to have ice cream, you probably can choose half & half, you can enjoy the feeling when you smell the chocolate smell and eat with it.  or you can enjoy beer or cake in "globe antiques (ザ グローブアン ティ ークス) .   also, we checked the Blue Bottle Coffee Sangenjaya ...