`

三Python内置函数

阅读更多

1. type

    type函数返回任意对象的数据类型

    

#-------------------------------------------------------------------------------
# coding:        utf-8
# Name:        内置函数
# Purpose:
#
# Author:      zdk
#
# Created:     18/02/2013
# Copyright:   (c) zdk 2013
#-------------------------------------------------------------------------------
import types
import TupleTest

if __name__ == '__main__':
    print(type(1))          #<class 'int'>
    print(type([]))         #<class 'list'>
    print(type("abc"))      #<class 'str'>
    print(type(TupleTest))  #<class 'module'>
    print(type(TupleTest) == types.ModuleType)  #True

    (1)type可以接收任何东西作为参数。 

    (2)type接收变量作为参数,并返回它的数据类型。

    (3)type还可以作用于模块。

    (4) 你可以使用types模块中的常量类进行对象类型的比较。

 

 

2.str

    str将数据强制转换成字符串,每种类型都可以。

    

    print(str(1))               # 1
    print(str(['noe','two']))   # ['noe','two']
    print(str(TupleTest))       # <module 'TupleTest' from 'D:\编程\python\codes\TupleTest.py'>
    print(str(None))            # None

    (1)可以作用于比较简单的数据类型比如整形。

    (2)可以作用于任何数据类型的任何对象,如列表。

    (3)str还允许作用于一个模块。

    (4)str还可以作用于None,None是Python的null,返回了字符串形式的None。

 

3.dir

    返回任意对象的属性和方法列表,包括模块对象、函数对象、字符串对象、列表对象、字典对象……相当多的东西

    

    print(dir([]))  #['__add__', '__class__', ……, 'remove', 'reverse', 'sort']
    print(dir({}))  #['__class__', '__contains__', '__delattr__',……, 'update', 'values']
    print(dir(TupleTest))#['__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', 'main']

    (1)dir([])返回一个包含所有列表方法的列表。只返回字符串形式的方法名称,而不是方法本身。

    (2)dir({})返回字典方法的名称列表。

    (3)dir(TupleTest)返回模块中定义的所有部件的列表,包括内置属性:__name__、__doc__ 。

 

4.callable

    接收任何对象作为参数,如果参数对象是可调用的,返回True;否则返回False

    

import string

if __name__ == '__main__':
    print(string.punctuation)           # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
    print(callable(string.punctuation)) # False
    print(callable(string.Formatter))   # True
    print(callable("".join))            # True

    (1) string模块已不赞成使用,但是这个模块中包含了许多有用的变量,string.punctuation,这个包含了所有标准的标点符号。

    (2)string.punctuaction是一个不可调用的对象;它是一个字符串。

    (3)string.Formatter是可调用的。

    (4)"".join是一个可调用的;这个函数接收2个参数。

 

5.getattr

    通过getattr获取对象引用

    

    li = ["one","two"]
    print(li.pop)            #<built-in method pop of list object at 0x01E8A490>
    print(getattr(li,"pop")) #<built-in method pop of list object at 0x01E8A490>
    getattr(li,"append")("three")
    print(li)       #['one', 'two', 'three']
    print(type(getattr(TupleTest,"main")) == types.FunctionType) #True
    print(getattr(li,"abc","defaultValue"))   #defaultValue

   (1)li.pop获取列表的pop方法的引用,getattr(li,"pop")也是获取列表pop方法的引用。

   (2)getattr(li,"append")("three") 获取列表的append方法的引用,并append一个three到li中。

   (3)getattr(li,"abc","defaultValue") 由于列表中没有abc属性,所以会返回默认的defaultValue。

 

6.过滤列表

    过来列表的语法:[mapping-expression for element in source-list if filter-expression]

    以if开头的是过滤表达式,过滤表达式返回值为真或假,在过滤表达式返回为真的元素都可以包含在映射中

    

    li = ['a','mpilgrim','foo','b','a','b','d','d']
    print([e for e in li if len(e)>1])       #['mpilgrim', 'foo']
    print([e for e in li if e!='d'])         #['a', 'mpilgrim', 'foo', 'b', 'a', 'b']
    print([e for e in li if li.count(e)==1]) #['mpilgrim', 'foo']

    (1)筛选出元素长度大于1的。

    (2)过滤掉d。

    (3)筛选出在列表中出现1次数的元素。

 

 

7.and 和 or的特殊性质

    在Python中,and和or执行逻辑演算,但它们并不返回布尔值,而是返回它们实际进行比较的值之一

    

    print('a' and 'b')          #b
    print('' and 'b')           #''
    print('a' and 'b' and 'c')  #c
    print('a' or 'b')           #a
    print('' or 'b')            #b
    print('' or [] or {})       #{}

    (1)使用and时,从左到右运算,0、''、[]、()、{}、None在布尔环境中为假,其他值都为真。

    (2)如果布尔环境中摸个值为假,则and返回第一个假值。

    (3)如果所有值都为真,and返回最后一个正值。

 

    (4)or同样是从左到右运算,如果有一个值为真,or立即返回该值。

    (5)所有值都为假,or返回最后一个假值。

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics