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