dir()
dir是内置函数,可以用它查看对象的属性及方法
- 方法一:直接输入dir() 查看当前作用域下的 属性及方法
[wangchao@bogon pyStu]$ python
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyword
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'keyword']
>>>
- 方法二
>>> import keyword #导入keyword模块,包含python里的所有关键字
>>> dir(keyword) #查看keyword所有的属性,及方法
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'iskeyword', 'kwlist', 'main']#根据输入内容发现有个kwlist
>>> keyword.kwlist #kwlist内容就是python所以的关键字
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
help()
在学习Python过程中,我们会遇到很多困惑的地方,通过Python自带的帮助函数是最快速最准确的。
方法一: 在Python交互模式下 ,输入 help() 进入在线帮助,按q 退出。示例中是查看系统自带的模块列表,其实远不止这些,这里只复制了一部分。当然还可以用help查看具体模块下的方法的使用说明。
>>> help()
Welcome to Python 2.7! This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
help> modules
Please wait a moment while I gather a list of all available modules...
dm.c: 1693: not running as root returning empty list
/usr/lib64/python2.7/site-packages/gobject/constants.py:24: Warning: g_boxed_type_register_static: assertion 'g_type_from_name (name) == 0' failed
import gobject._gobject
BaseHTTPServer cairo itertools robotparser
Bastion calendar javapackages rpm
CDROM caribou json rpmUtils
CGIHTTPServer cgi keyword runpy
ConfigParser cgitb kitchen scanext
Cookie chardet langtable sched
base64 idlelib pytz xml
bdb ihooks pyudev xmllib
binascii imageop quopri xmlrpclib
binhex imaplib random xxsubtype
bisect imghdr re yaml
blivet imp readline yum
block importlib report yumutils
brlapi imputil reportclient zipfile
bsddb iniparse repr zipimport
bz2 initial_setup resource zlib
cPickle inspect rexec
cProfile io rfc822
cStringIO ipaddress rlcompleter
方法二: 直接在交互模式下输入 help(模块名) ,前提是必须import模块才行 (常用)
>>> import math
>>> help(math)
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos(...)
acos(x)
Return the arc cosine (measured in radians) of x.
acosh(...)
acosh(x)
Return the hyperbolic arc cosine (measured in radians) of x.
asin(...)
asin(x)
Return the arc sine (measured in radians) of x.
asinh(...)
asinh(x)
Return the hyperbolic arc sine (measured in radians) of x.
:
type()
type() 函数有助于我们确定对象是字符串还是整数,或者是其它类型。
>>> import types
>>> dir(types)
['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> p = "I love Python"
>>> type(p)
<type 'str'>
>>> if type(p) is types.StringType:
... print "p is a string"
...
p is a string
>>> type(42)
<type 'int'>
>>> type([])
<type 'list'>
>>> type({})
<type 'dict'>
>>> type(dir)
<type 'builtin_function_or_method'>
callable()
callable可以测试对象潜在行为(函数和方法)是否可以调用。
>>> print callable.__doc__
callable(object) -> bool
Return whether the object is callable (i.e., some kind of function).
Note that classes are callable, as are instances with a __call__() method.
>>> callable("a string")
False
>>> callable(dir)
True
id()
id用于分析深拷贝和浅拷贝
>>> lst = [1,2,3]
>>> lst2 = lst
>>> import copy
>>> dir(copy)
['Error', 'PyStringMap', '_EmptyClass', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_dispatch', '_copy_immutable', '_copy_inst', '_copy_with_constructor', '_copy_with_copy_method', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_inst', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', '_test', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']
>>> lst3 = copy.copy(lst)
>>> id(lst)
140468834921648
>>> id(lst2)
140468834921648
>>> id(lst3)
140468841815288