stckiss 发表于 2013-1-27 04:49:31

python 内置函数

本文主要是学习随笔.记录下来以免忘记.....
1. type 函数
    返回任意对象的数据类型
>>> type(1)<type 'int'>>>> type(2.0000)<type 'float'>>>> type('ssss')<type 'str'>>>> ls=[]>>> type(ls)<type 'list'>>>> lse={}>>> type(lse)<type 'dict'>>>> import string>>> type(string)>>> type('strs') == types.StringTypeTrue2 str 函数
    将数据强制转换为字符串。每种类型都可以将数据强制转换为字符串。
 
>>> str()''>>> str(1)'1'>>> str('asb')'asb'>>> li=['sss',1,'000']>>> str(li)"['sss', 1, '000']">>> str(string)"<module 'string' from 'C:\\Python25\\lib\\string.pyc'>">>> str(sys)"<module 'sys' (built-in)>">>> str(None)'None'    None是python 的null 值
3 dir 函数
    函数返回任意对象的属性和方法列表, 包括模块对象 、函数字符串列表典…… 相当多的包括模块对象 、函数字符串列表典…… .
>>> dir([])['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']>>> dir({})['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']4 callable 函数
    接收任何对象作为参如果是可调用的函数,返回 True;否则返回 False. 可调用对象包括函数,类方法,甚至类自身.....
 5.getattr 函数
    得到运行时函数对象引用..相当于javascript eval()方法.但是有所不同
>>> li=['a','b']>>> li.pop<built-in method pop of list object at 0x012A2F58>>>> getattr(li,'pop')<built-in method pop of list object at 0x012A2F58>>>> getattr(li,'append')('c')>>> li['a', 'b', 'c']>>> li.append('d')>>> li['a', 'b', 'c', 'd']>>>  所有内置函数都在__builtin__模块下面
 
  
>>> dir(__builtin__)['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', 'abs', 'all', 'any', 'apply', 'basestring', 'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']>>>  以上只为记录....要学习的地方还多.脚本语言不比java c/c++ 容易到那去..许多技巧性的东东还要了解...
页: [1]
查看完整版本: python 内置函数