「簡明pythonチュートリアル」のまとめ(3)--関数、モジュール

22415 ワード

関数#カンスウ#
 
関数パス
defキーワード定義、パラメータは関数定義のカッコ対内で指定し、カンマで分割する
 
例:
#!/usr/bin/python
# Filename: func_param.py
def printMax(a, b):
    if a > b:
        print a, 'is maximum'
    else:
        print b, 'is maximum'
printMax(3, 4)# directly give literal values
x = 5
y = 7
printMax(x, y)

 
しゅつりょく
$ python func_param.py
4 is maximum
7 is maximum
 
グローバル とローカル
 
ローカル
で を する 、
で じ を つ の とは の もありません.すなわち、 は にとってローカルです.
#!/usr/bin/python
# Filename: func_local.py
def func(x) :
     print 'x is', x
    x = 2
     print 'Changed local x to', x
x = 50
func(x)
print 'x is still', x
$ python func_local.py
x is 50
Changed local x to 2
x is still 50
 
, Python , 。 global , 。 global ,
#!/usr/bin/python
# Filename: func_global.py
def func() :
     global x
     print 'x is', x
    x = 2
     print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x
$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2
 
。 ,
: , 。 C/C++ 。 func(a,b=1,c=2) ,func(a=1,b,c=2)
 
, , —— —— ( ) ( )
 
#!/usr/bin/python
# Filename: func_key.py
def func(a, b = 5, c = 10) :
     print 'a is', a, 'and b is', b, 'and c is', c
func( 3, 7)
func( 25, c = 24)
func(c = 50, a = 100)
$ python func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
 
Docstrings--
DocStrings , , 。 , !
#!/usr/bin/python
# Filename: func_doc.py
def printMax(x, y) :
     '' 'Prints the maximum of two numbers.
    The two values must be integers.'
''
    x = int(x) # convert to integers, if possible
    y = int(y)
     if x > y :
         print x, 'is maximum'
     else :
         print y, 'is maximum'
printMax( 3, 5)
print printMax.__doc__
$ python func_doc.py
5 is maximum
Prints the maximum of two numbers.
        The two values must be integers.
 
。 ,DocStrings
, , 。 , 。
__doc__ ( ) printMax ( )。
help() __doc__

 
。 , .py 。
: 。 。
 
: sys
#!/usr/bin/python
# Filename: using_sys.py
import sys
print 'The command line arguments are:'
for i in sys.argv :
     print i
print '

The PYTHONPATH is'
, sys.path, '
'
$ python using_sys.py we are arguments
The command line arguments are:
using_sys.py
we
are
arguments
The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip',
'/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
'/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']
 
import sys  Python, sys 。 sys Python 。
Python import sys , sys.path sys.py 。 , , 。
sys.path 。sys.path —— sys.path , PYTHONPATH 。 。 , sys.path
 
.pyc
 
,Python , 。
, .pyc 。 Python ( Python ?)。 ,.pyc —— , 。 , 。
 
 
#!/usr/bin/python
# Filename: mymodule.py
def sayhi() :
     print 'Hi, this is mymodule speaking.'
version = '0.1'
# End of mymodule.py
, sys.path 。
 
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1
 
  from...import 
(from x import a,b,c  x / a,b,c)
#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, version
# Alternative:
# from mymodule import *
sayhi()
print 'Version', version
 
dir()
dir 。 、 。
dir() , 。 ,
 
:
#!/usr/bin/env python
#filename:mymodule.py
import sys
def sayhi() :
     print 'Hi, this is mymodule speaking.'
 
version = '0.1'
 
print dir()
print dir( sys)
: [] , [] sys
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'sayhi', 'sys', 'version']
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']