免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2485 | 回复: 0

自省的威力 [复制链接]

论坛徽章:
0
发表于 2009-12-01 17:09 |显示全部楼层
使用可选参数和命名参数def info(object, spacing=10, collapse=1)spacing collapse是可选参数,object是必备参数,有1个参数时候是object,2个时候有spacing,collapse还是缺省的,默认为1。def info(spacing=15, object=odbchelper)甚至必本参数也可以采用命名参数的形式, 而且参数不存在顺序问题。
使用type, str, dir 和其他内置函数type函数>>> type(1)>>> li = []>>> type(li)>>> import odbchelper>>> type(odbchelper)>>> import types>>> type(odbchelper) == types.ModuleTypeTruetype可以接受任何东西作为参数,并且返回他的数据类型。整型,字符串,list等等。
str介绍str强制将数据转化为字符串,每种数据类型都可以强制转化为字符串。>>> str(1)'1'>>> horsemen = ['war', 'pestilence', 'famine']>>> horsemen['war', 'pestilence', 'famine']>>> horsemen.append('Powerbuilder')>>> str(horsemen)"['war', 'pestilence', 'famine', 'Powerbuilder']">>> str(odbchelper)"">>> str(None)'None'
dir介绍dir函数返回任意对象的属性和方法列表,包含模块对象,函数对象,字符串对象等等。>>> li = []>>> dir(li)['append', 'count', 'extend', 'index', 'insert','pop', 'remove', 'reverse', 'sort']>>> d = {}>>> dir(d)['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault', 'update', 'values']>>> import odbchelper>>> dir(odbchelper)['__builtins__', '__doc__', '__file__', '__name__', 'buildConnectionString']dir(odbchelper)返回模块中定义的所有部件的列表,包括内置属性,例如__name__,__doc__以及其他定义的属性和方法。
callable介绍>>>import string>>>string.punctuation'!"#$%&\'()*+,-./:;?@[\\]^_`{|}~'>>>string.join>>>callable(string.punctuation)False>>>callable(string.join)True>>> print string.join.__doc__join(list [,sep]) -> string  Return a string composed of the words in list, with  intervening occurrences of sep. The default separator is a  single space.  (joinfields and join are synonymous)
string.punctuation是不可调用的对象,他是一个字符串string.join是可调用的;这个函数可以接受2个参数。任何可调用的对象都有doc string。通过将callable函数作用于一个对象的每个属性。
内置函数type, str, dir和其他的python内置函数都归组到了__builtin__这个特殊的模块中。可以认为python启动时候自动执行了from __builtin__ import *,此语句将所有内置函数导入命名空间,所以在命名空间,可以使用所有内置函数。
通过getattr获取对象引用使用getattr可以得到一个直到运行时才知道名称的函数的引用。>>> li = ["Larry", "Curly"]>>> li.pop>>> getattr(li, "pop")>>> getattr(li, "append")("Moe")>>> li["Larry", "Curly", "Moe"]>>> getattr({}, "clear")
过滤列表python有能把列表映射到其他列表的能力。这种能力同过滤机制结合使用,能让列表的有些元素被映射同时跳过一些元素。>>> li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]>>> [elem for elem in li if len(elem) > 1]['mpilgrim', 'foo']>>> [elem for elem in li if elem != "b"]['a', 'mpilgrim', 'foo', 'c', 'd', 'd']>>> [elem for elem in li if li.count(elem) == 1]['a', 'mpilgrim', 'foo', 'c']count是一个列表方法,返回某个值在列表中出现的次数。去除出现2次的元素。可以认为此题为去除重复的元素。
and 和 or 的特殊性质>>>'a' and 'b''b'>>>and 'b'''>>>'a' and 'b' and 'c''c'使用and时候,从左到右演算表达式的值。0,'',[],{},None在布尔环境中为假,其他都为真。如果两个要都是真,返回最后1个。
>>>'a' or 'b''a'>>>'' or 'b''b'>>>'' or [] or {}{}使用or时候,从左到右演算表达式的值。碰见真,立刻停止。如果都为假,返回最后1个。
ljust介绍>>>s = 'buildConnectionString'>>>s.ljust(30)'buildConnectionString     '
ljust用空格填充字符串以符合指定的长度。如果指定长度小于字符串长度,则返回字符串,不会擅自截断。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103763/showart_2108653.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP