Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230675
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-01 18:05
文章分类

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-10-24 17:12:56

合理使用python提供的内置函数提供的信息,可以很好的帮助我们编程,这里介绍3个函数:type(),dir(),help()

type(x)
type()可以接收任何东西作为参数――并返回它的数据类型。
整型、字符串、列表、字典、元组、函数、类、模块,甚至类型对象都可以作为参数被 type 函数接受。

点击(此处)折叠或打开

  1. In [66]: type(2)
  2. Out[66]: int

  3. In [67]: type('ssdgg')
  4. Out[67]: str

  5. In [68]: alist = []

  6. In [69]: type(alist)
  7. Out[69]: list

  8. In [70]: import sys

  9. In [71]: type(sys)
  10. Out[71]: module
dir()函数使用方法
使用dir()函数可以查看对像内所有属于及方法,在python中任何东西都是对像,一种,一个等,都有自己的属性和方法,除了常用方法外,其它的你不需要全部记住它,交给dir()函数就好了。dir()函数操作方法很简单,只需要把你想要查询和对像添写到( )括号中就可以使用了,它返回包含要查询对象的所有属性名称的列表

点击(此处)折叠或打开

  1. In [107]: dir([])
  2. Out[107]: 
  3. ['__add__',
  4.  '__class__',
  5.  '__contains__',
  6.  '__delattr__',
  7.  '__delitem__',
  8.  '__delslice__',
  9.  '__doc__',
  10.  '__eq__',
  11.  '__format__',
  12.  '__ge__',
  13.  '__getattribute__',
  14.  '__getitem__',
  15.  '__getslice__',
  16.  '__gt__',
  17.  '__hash__',
  18.  '__iadd__',
  19.  '__imul__',
  20.  '__init__',
  21.  '__iter__',
  22.  '__le__',
  23.  '__len__',
  24.  '__lt__',
  25.  '__mul__',
  26.  '__ne__',
  27.  '__new__',
  28.  '__reduce__',
  29.  '__reduce_ex__',
  30.  '__repr__',
  31.  '__reversed__',
  32.  '__rmul__',
  33.  '__setattr__',
  34.  '__setitem__',
  35.  '__setslice__',
  36.  '__sizeof__',
  37.  '__str__',
  38.  '__subclasshook__',
  39.  'append',
  40.  'count',
  41.  'extend',
  42.  'index',
  43.  'insert',
  44.  'pop',
  45.  'remove',
  46.  'reverse',
  47.  'sort']
要查看python某个模块可以干什么,先要导入模块,之后用上面讲过的方式查看就可以。比如要查看都可以干什么,可以像下边这样操作:

点击(此处)折叠或打开

  1. In [108]: import sys

  2. In [109]: dir(sys)
  3. Out[109]: 
  4. ['__displayhook__',
  5.  '__doc__',
  6.  '__excepthook__',
  7.  '__name__',
  8.  '__package__',
  9.  '__stderr__',
  10.  '__stdin__',
  11.  '__stdout__',
  12.  '_clear_type_cache',
  13.  '_current_frames',
  14.  '_getframe',
  15.  'api_version',
  16.  'argv',
  17.  'builtin_module_names',
  18.  'byteorder',
  19.  'call_tracing',
  20.  'callstats',
  21.  'copyright',
  22.  'displayhook',
  23.  'dont_write_bytecode',
  24.  'exc_clear',
  25.  'exc_info',
  26.  'exc_traceback',
  27.  'exc_type',
  28.  'exc_value',
  29.  'excepthook',
  30.  'exec_prefix',
  31.  'executable',
  32.  'exit',
  33.  'exitfunc',
  34.  'flags',
  35.  'float_info',
  36.  'getcheckinterval',
  37.  'getdefaultencoding',
  38.  'getdlopenflags',
  39.  'getfilesystemencoding',
  40.  'getprofile',
  41.  'getrecursionlimit',
  42.  'getrefcount',
  43.  'getsizeof',
  44.  'gettrace',
  45.  'hexversion',
  46.  'last_traceback',
  47.  'last_type',
  48.  'last_value',
  49.  'maxint',
  50.  'maxsize',
  51.  'maxunicode',
  52.  'meta_path',
  53.  'modules',
  54.  'path',
  55.  'path_hooks',
  56.  'path_importer_cache',
  57.  'platform',
  58.  'prefix',
  59.  'ps1',
  60.  'ps2',
  61.  'ps3',
  62.  'py3kwarning',
  63.  'setcheckinterval',
  64.  'setdlopenflags',
  65.  'setprofile',
  66.  'setrecursionlimit',
  67.  'settrace',
  68.  'stderr',
  69.  'stdin',
  70.  'stdout',
  71.  'subversion',
  72.  'version',
  73.  'version_info',
  74.  'warnoptions']

help()函数的作用
在使用python来编写代码时,会经常使用python自带函数或模块,一些不常用的函数或是模块的用途不是很清楚,这时候就需要用到help函数来查看帮助。
这里要注意下,help()函数是查看函数或模块用途的详细说明,而dir()函数是查看函数或模块内的操作方法都有什么,输出的是方法列表。
help()括号内填写参数,操作方法很简单。例如:

点击(此处)折叠或打开

  1. In [110]: help('dir')
  2. Help on built-in function dir in module __builtin__:

  3. dir(...)
  4.     dir([object]) -> list of strings
  5.     
  6.     If called without an argument, return the names in the current scope.
  7.     Else, return an alphabetized list of names comprising (some of) the attributes
  8.     of the given object, and of attributes reachable from it.
  9.     If the object supplies a method named __dir__, it will be used; otherwise
  10.     the default dir() logic is used and returns:
  11.       for a module object: the module's attributes.
  12.       for a class object: its attributes, and recursively the attributes
  13.         of its bases.
  14.       for any other object: its attributes, its class's attributes, and
  15.         recursively the attributes of its class
使用help函数查看帮助实例:
在写help()函数使用方法时说过,括号中填写参数,那在这里要注意参数的形式:
1.查看一个模块的帮助

点击(此处)折叠或打开

  1. In [1]: help('sys')
  2. Help on built-in module sys:

  3. NAME
  4.     sys

  5. FILE
  6.     (built-in)

  7. MODULE DOCS
  8.     http://docs.python.org/library/sys

  9. DESCRIPTION
  10.     This module provides access to some objects used or maintained by the
  11.     interpreter and to functions that interact strongly with the interpreter.
  12.     
  13.     Dynamic objects:
2.查看一个数据类型的帮助

点击(此处)折叠或打开

  1. In [1]: help('str')
  2. Help on class str in module __builtin__:

  3. class str(basestring)
  4.  | str(object) -> string
  5.  |
  6.  | Return a nice string representation of the object.
  7.  | If the argument is a string, the return value is the same object.
  8.  |
  9.  | Method resolution order:
  10.  | str
  11.  | basestring
  12.  | object
  13.  |
  14.  | Methods defined here:
  15.  |
  16.  | __add__(...)
  17.  | x.__add__(y) <==> x+y
  18.  |
  19.  | __contains__(...)
  20.  | x.__contains__(y) <==> y in x
  21.  |
  22.  | __eq__(...)
  23.  | x.__eq__(y) <==> x==y

  24. [2]+ Stopped ipython
  25. [root@ftp ~]# ipython
  26. Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
  27. Type "copyright", "credits" or "license" for more information.

  28. IPython 1.2.1 -- An enhanced Interactive Python.
  29. ? -> Introduction and overview of IPython's features.
  30. %quickref -> Quick reference.
  31. help -> Python's own help system.
  32. object? -> Details about 'object', use 'object??' for extra details.
返回字符串的方法及详细说明

点击(此处)折叠或打开

  1. a = [1,2,3]
  2. help(a)
  3. Help on list object:
  4. class list(object)
  5.  | list() -> new empty list
  6.  | list(iterable) -> new list initialized from iterable's items
  7.  |
  8.  | Methods defined here:
  9.  |
  10.  | __add__(...)
  11.  | x.__add__(y) <==> x+y
  12.  |
  13.  | __contains__(...)
  14.  | x.__contains__(y) <==> y in x
  15.  |
  16.  | __delitem__(...)
  17.  | x.__delitem__(y) <==> del x[y]
  18.  |
  19.  | __delslice__(...)
  20.  | x.__delslice__(i, j) <==> del x[i:j]
  21.  |
  22.  | Use of negative indices is not supported.
  23.  |
  24.  | __eq__(...)
  25.  | x.__eq__(y) <==> x==y
  26.  |
  27.  | __ge__(...)
  28.  | x.__ge__(y) <==> x>=y
  29.  |
  30.  | __getattribute__(...)
  31.  | x.__getattribute__('name
这时help(a)则会打开list的操作方法

点击(此处)折叠或打开

  1. help(a.append)
  2. Help on built-in function append:
  3. append(...)
  4.     L.append(object) -- append object to end











阅读(1418) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~