Python菜鸟之路:Python基础——函数

一、函数

1. 简介

  函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率

2. 组成

  • 函数代码块以 def 关键词开头,后接函数名圆括号()
  • 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数
  • 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
  • 函数主体部分:函数内容以冒号起始,并且缩进。
  • 函数结束部分:return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。

示例:

1 def functionname( parameters ):
2    "函数声明、注释等内容,一般为__doc__部分"
3    函数主体部分
4    return [expression]

3. 简单调用

  以上边的示例为例,调用方法很简单执行如下代码。

   functionname(参数) 

  注意,函数定义好之后,并不会运行。只有在调用的时候会运行。

二、函数各组成部分

2.1 函数的命名

  函数名应该为小写,可以用下划线风格单词以增加可读性。如:myfunction,my_example_function。

Python之父Guido推荐的命名规范包括如下几点:
模块名和包名采用小写字母并且以下划线分隔单词的形式;
类名采用以大写字母开头,并且以大写字母分隔单词的形式命名;
全局或者类常量,全部使用大写字母,并且以下划线分隔单词;其余变量命名则是采用全部小写字母,并且以下划线分隔单词的形式命名。
以上的内容如果是内部的,则使用下划线开头命名。

2.2 函数参数

  函数的参数分为三类:普通参数、默认参数、指定参数、可变参数

2.2.1 普通参数

1 def functionname(name,age):
2     print("I'm %s, age %s" % (name, age))
3 
4 functionname("wzg",18)
5 out: I'm wzg, age 18

2.2.2 默认参数

1 def functionname(name,age=18):
2     print("I'm %s, age %s" % (name, age))
3  
4 functionname("wzg")
5 out: I'm wzg, age 18

2.2.3 指定参数

1 def functionname(name,age=18):
2     print("I'm %s, age %s" % (name, age))
3  
4 functionname(age=18, name="wzg")
5 out: I'm wzg, age 18

2.2.4 可变参数

2.2.4.1 可变参数*

1 def function_name(*args):
2     print(args, type(args))
3 
4 function_name(1,2,3)
5 out: (1, 2, 3) <class 'tuple'>
6 
7 function_name((1,2,3))
8 out: ((1, 2, 3),) <class 'tuple'>

  由上边的例子可以看出,默认将传入的参数,全部放在元组中,即args = (...),在执行()的时候,会进行tuple.__init__方法。

1 def function_name(*args):
2     print(args, type(args))
3 function_name(*(1,2,3))
4 
5 out: (1, 2, 3) <class 'tuple'>
6 
7 function_name(*'wzg')
8 
9 out: ('w', 'z', 'g') <class 'tuple'>

  从上边的例子,可以看出带*的参数,会循环变量中的每个元素加入至tuple中。字符串的话循环每个字母。

2.2.4.2 可变参数**

1 def function_name(**args):
2     print(args, type(args))
3 function_name(name='wzg')
4 out: {'name': 'wzg'} <class 'dict'>
5 dic={"k1":"v1","k2":"v2"}
6 function_name(**dic)
7 out: {'k1': 'v1', 'k2': 'v2'} <class 'dict'>
8 function_name(d1=dic)
9 out: {'d1': {'k1': 'v1', 'k2': 'v2'}} <class 'dict'>

  从上边的例子可以看出,可变参数** , 默认将参数的参数,全部放在字典中进行处理。

2.3 函数注释

  Python有一种独一无二的的注释方式: 使用文档字符串. 文档字符串是包, 模块, 类或函数里的第一个语句. 这些字符串可以通过对象的__doc__成员被自动提取, 并且被pydoc所用。参照下面的一个代码

 1 def function_name(big_table, keys, other_silly_variable=None):
 2     """Fetches rows from a Bigtable.
 3 
 4     Retrieves rows pertaining to the given keys from the Table instance
 5     represented by big_table.  Silly things may happen if
 6     other_silly_variable is not None.
 7 
 8     Args:
 9         big_table: An open Bigtable Table instance.
10         keys: A sequence of strings representing the key of each table row
11             to fetch.
12         other_silly_variable: Another optional variable, that has a much
13             longer name than the other args, and which does nothing.
14 
15     Returns:
16         A dict mapping keys to the corresponding table row data
17         fetched. Each row is represented as a tuple of strings. For
18         example:
19 
20         {'Serak': ('Rigel VII', 'Preparer'),
21          'Zim': ('Irk', 'Invader'),
22          'Lrrr': ('Omicron Persei 8', 'Emperor')}
23 
24         If a key from the keys argument is missing from the dictionary,
25         then that row was not found in the table.
26 
27     Raises:
28         IOError: An error occurred accessing the bigtable.Table object.
29     """
30     function_body
31     return [expression]
注释

  从例子中,可以看出函数注释包含以下几个部分:

  1.整体功能说明  2.输入参数说明  3.输出/返回值说明  4.异常说明  5.其他

2.4 函数主体

  函数主体部分就是代码逻辑的实现/处理过程。

2.5 函数返回值

  函数返回值是一个可选的选项,可以返回一个表达式、某种数据结构等。默认返回None

三、函数的分类

   函数大概可以分为以下几类

  • 內建函数
  • 自定义函数
  • 匿名函数

 3.1 內建函数 __builtins__

  从Python3.5官网拔下来一张最新的内置函数列表

  与2.7Python相比:

  新增:ascii() ,bytes() , exec(),
  减少:basestring() ,cmp(), execfile(), file(),long(),raw_input(),reduce(), reload() , unichr(), unicode() ,xrange()

3.2 常用內建函数

函数名 作用
all(iterable) 1、集合中的元素都为真的时候为真  2、特别的,若为空串返回为True
any(iterable) 1、集合中的元素有一个为真的时候为真 2、特别的,若为空串返回为False
bool([x]) 将x转换为Boolean类型
ascii() 只要执行这个方法,则会自动调用对象的__repr__。这个函数跟repr()函数一样,返回一个可打印的对象字符串方式表示。当遇到非ASCII码时,就会输出x,u或U等字符来表示。与Python 2版本里的repr()是等效的函数。
abs(x) 求绝对值
pow(x, y[, z])  返回x的y次幂
oct(x) 将一个数字转化为8进制
hex(x) 将整数x转换为16进制字符串
bin(x) 将整数x转换为二进制字符串
bytes("要转化的字符串", encoding="编码") 字符串转换为字节类型

 3.3 自定义函数

  我们平时使用的大多数函数,以及开发中创建的函数,都属于自定义函数。这极大的提高了代码的重用性和可读性。

      自定义函数的创建和使用,在上文中已经进行了说明和示例,参照上边文章即可。这里不作过多说明。

3.4 匿名函数

  python 使用 lambda 来创建匿名函数。

  • lambda只是一个表达式,函数体比def简单很多。
  • lambda的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。
  • lambda函数拥有自己的命名空间,且不能访问自有参数列表之外或全局命名空间里的参数。
  • 虽然lambda函数看起来只能写一行,却不等同于C或C++的内联函数,后者的目的是调用小函数时不占用栈内存从而增加运行效率。

3.4.1 匿名函数创建语法

    lambda [arg1 [,arg2,.....argn]]:expression

3.4.2 示例

>>> lambda x: x+1 #一个参数
>>> lambda x,y,z:x+y+z #多个参数

>>> lambda x,y=3: x*y #允许参数存在默认值,但是默认值的参数必须参数顺序最后

 1 a= lambda x,y=3: x*y
 2 print(a(4))
 3 
 4 out: 12
 5 
 6 print(a(2,4))
 7 
 8 out: 8
 9 
10 b= lambda x,y,z: x*y*z
11 print(b(1,2,3))
12 
13 out: 6
lambda

四、作用域

  python中的作用域分4种情况:

  L:local,局部作用域,即函数中定义的变量;
  E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,但不是全局的;
  G:global,全局变量,就是模块级别定义的变量;

  B:built-in,系统固定模块里面的变量,比如int, bytearray等。

搜索变量的优先级顺序依次是:作用域局部>外层作用域>当前模块中的全局>python内置作用域,也就是LEGB。

  Python中,是以函数作为作用域的,没有块级作用域的概念,这点同js一样。这里的块级是只if.while,for等语句。要深入理解Python作用域,有以下几个要点:

    1. Python中存在作用域链,查找顺序为由内到外,直到查不到时报错not defined
    2. Python的作用域,在函数没有执行之前就已经全部确定,作用域链也已经确定
    3. Python函数在没有执行之前,函数内部不执行。而部分作用域的情况,是需要去看到底有没有执行。

具体代码如下:

name = 'boss'
def f1():
    print(name)

def f2():
    name = 'eric'
    return f1

ret = f2()
ret()   
# out: boss

#-------------------------
name = 'boss'
def f1():
    print(name)

def f2():
    name = 'eric'
    f1()
f2()
# out: boss
作用域链案例
li = []
for i in range(10):
    def f1():
        return i
    li.append(f1)
print(li[0]())  # out: 9
print(li[1]())  # out: 9
print(li[2]())  # out: 9
print(li[3]())  # out: 9

#--------------------------------本质去看有没有执行
li = []
for i in range(10):
    def f1(x=i):
        return x
    li.append(f1)
print(li[0]())  # out: 0
print(li[1]())  # out: 1
print(li[2]())  # out: 2
print(li[3]())  # out: 3
函数在没执行之前,内部不执行案例1
1 li =[lambda  :x for x in range(10)]
2 
3 ret = li[0]()
4 print(ret) # out: 9
5 ret = li[1]()
6 print(ret) # out: 9
7 ret = li[2]()
8 print(ret) # out: 9
函数在没执行之前,内部不执行案例2

五、文件操作

操作文件,一般需要经过三大步骤

  1. 打开文件

  2. 操作文件

  3. 关闭文件(非必须)

3.1 打开文件

  打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。通常我们使用open()函数来打开文件,源码中说明了打开模式:

  1 def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open
  2     """
  3     Open file and return a stream.  Raise IOError upon failure.
  4     
  5     file is either a text or byte string giving the name (and the path
  6     if the file isn't in the current working directory) of the file to
  7     be opened or an integer file descriptor of the file to be
  8     wrapped. (If a file descriptor is given, it is closed when the
  9     returned I/O object is closed, unless closefd is set to False.)
 10     
 11     mode is an optional string that specifies the mode in which the file
 12     is opened. It defaults to 'r' which means open for reading in text
 13     mode.  Other common values are 'w' for writing (truncating the file if
 14     it already exists), 'x' for creating and writing to a new file, and
 15     'a' for appending (which on some Unix systems, means that all writes
 16     append to the end of the file regardless of the current seek position).
 17     In text mode, if encoding is not specified the encoding used is platform
 18     dependent: locale.getpreferredencoding(False) is called to get the
 19     current locale encoding. (For reading and writing raw bytes use binary
 20     mode and leave encoding unspecified.) The available modes are:
 21     
 22     ========= ===============================================================
 23     Character Meaning
 24     --------- ---------------------------------------------------------------
 25     'r'       open for reading (default)
 26     'w'       open for writing, truncating the file first
 27     'x'       create a new file and open it for writing 如果文件存在则报错
 28     'a'       open for writing, appending to the end of the file if it exists
 29     'b'       binary mode
 30     't'       text mode (default)
 31     '+'       open a disk file for updating (reading and writing)
 32     'U'       universal newline mode (deprecated)
 33     ========= ===============================================================
 34     
 35     The default mode is 'rt' (open for reading text). For binary random
 36     access, the mode 'w+b' opens and truncates the file to 0 bytes, while
 37     'r+b' opens the file without truncation. The 'x' mode implies 'w' and
 38     raises an `FileExistsError` if the file already exists.
 39     
 40     Python distinguishes between files opened in binary and text modes,
 41     even when the underlying operating system doesn't. Files opened in
 42     binary mode (appending 'b' to the mode argument) return contents as
 43     bytes objects without any decoding. In text mode (the default, or when
 44     't' is appended to the mode argument), the contents of the file are
 45     returned as strings, the bytes having been first decoded using a
 46     platform-dependent encoding or using the specified encoding if given.
 47     
 48     'U' mode is deprecated and will raise an exception in future versions
 49     of Python.  It has no effect in Python 3.  Use newline to control
 50     universal newlines mode.
 51     
 52     buffering is an optional integer used to set the buffering policy.
 53     Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
 54     line buffering (only usable in text mode), and an integer > 1 to indicate
 55     the size of a fixed-size chunk buffer.  When no buffering argument is
 56     given, the default buffering policy works as follows:
 57     
 58     * Binary files are buffered in fixed-size chunks; the size of the buffer
 59       is chosen using a heuristic trying to determine the underlying device's
 60       "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
 61       On many systems, the buffer will typically be 4096 or 8192 bytes long.
 62     
 63     * "Interactive" text files (files for which isatty() returns True)
 64       use line buffering.  Other text files use the policy described above
 65       for binary files.
 66     
 67     encoding is the name of the encoding used to decode or encode the
 68     file. This should only be used in text mode. The default encoding is
 69     platform dependent, but any encoding supported by Python can be
 70     passed.  See the codecs module for the list of supported encodings.
 71     
 72     errors is an optional string that specifies how encoding errors are to
 73     be handled---this argument should not be used in binary mode. Pass
 74     'strict' to raise a ValueError exception if there is an encoding error
 75     (the default of None has the same effect), or pass 'ignore' to ignore
 76     errors. (Note that ignoring encoding errors can lead to data loss.)
 77     See the documentation for codecs.register or run 'help(codecs.Codec)'
 78     for a list of the permitted encoding error strings.
 79     
 80     newline controls how universal newlines works (it only applies to text
 81     mode). It can be None, '', '
', '
', and '
'.  It works as
 82     follows:
 83     
 84     * On input, if newline is None, universal newlines mode is
 85       enabled. Lines in the input can end in '
', '
', or '
', and
 86       these are translated into '
' before being returned to the
 87       caller. If it is '', universal newline mode is enabled, but line
 88       endings are returned to the caller untranslated. If it has any of
 89       the other legal values, input lines are only terminated by the given
 90       string, and the line ending is returned to the caller untranslated.
 91     
 92     * On output, if newline is None, any '
' characters written are
 93       translated to the system default line separator, os.linesep. If
 94       newline is '' or '
', no translation takes place. If newline is any
 95       of the other legal values, any '
' characters written are translated
 96       to the given string.
 97     
 98     If closefd is False, the underlying file descriptor will be kept open
 99     when the file is closed. This does not work when a file name is given
100     and must be True in that case.
101     
102     A custom opener can be used by passing a callable as *opener*. The
103     underlying file descriptor for the file object is then obtained by
104     calling *opener* with (*file*, *flags*). *opener* must return an open
105     file descriptor (passing os.open as *opener* results in functionality
106     similar to passing None).
107     
108     open() returns a file object whose type depends on the mode, and
109     through which the standard file operations such as reading and writing
110     are performed. When open() is used to open a file in a text mode ('w',
111     'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open
112     a file in a binary mode, the returned class varies: in read binary
113     mode, it returns a BufferedReader; in write binary and append binary
114     modes, it returns a BufferedWriter, and in read/write mode, it returns
115     a BufferedRandom.
116     
117     It is also possible to use a string or bytearray as a file for both
118     reading and writing. For strings StringIO can be used like a file
119     opened in a text mode, and for bytes a BytesIO can be used like a file
120     opened in a binary mode.
121     """
122     pass
open函数3.5.1源码
模式描述
r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+ 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。这种方法基本不用
wb+ 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+

以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

x
如果文件存在则报错,如果不存在,则创建文件并写内容

3.2 操作文件

  创建文件句柄之后,可以针对句柄对文件进行如下操纵

  1 class TextIOWrapper(_TextIOBase):
  2     """
  3     Character and line based layer over a BufferedIOBase object, buffer.
  4     
  5     encoding gives the name of the encoding that the stream will be
  6     decoded or encoded with. It defaults to locale.getpreferredencoding(False).
  7     
  8     errors determines the strictness of encoding and decoding (see
  9     help(codecs.Codec) or the documentation for codecs.register) and
 10     defaults to "strict".
 11     
 12     newline controls how line endings are handled. It can be None, '',
 13     '
', '
', and '
'.  It works as follows:
 14     
 15     * On input, if newline is None, universal newlines mode is
 16       enabled. Lines in the input can end in '
', '
', or '
', and
 17       these are translated into '
' before being returned to the
 18       caller. If it is '', universal newline mode is enabled, but line
 19       endings are returned to the caller untranslated. If it has any of
 20       the other legal values, input lines are only terminated by the given
 21       string, and the line ending is returned to the caller untranslated.
 22     
 23     * On output, if newline is None, any '
' characters written are
 24       translated to the system default line separator, os.linesep. If
 25       newline is '' or '
', no translation takes place. If newline is any
 26       of the other legal values, any '
' characters written are translated
 27       to the given string.
 28     
 29     If line_buffering is True, a call to flush is implied when a call to
 30     write contains a newline character.
 31     """
 32     def close(self, *args, **kwargs): # real signature unknown
 33         pass '''关闭文件句柄'''
 34 
 35     def detach(self, *args, **kwargs): # real signature unknown
 36         pass '''移除掉已存在的文本编码层'''
 37 
 38     def fileno(self, *args, **kwargs): # real signature unknown
 39         pass '''返回所使用的底层实现请求从操作系统I/O操作的整数文件描述符'''
 40 
 41     def flush(self, *args, **kwargs): # real signature unknown
 42         pass '''将缓冲区数据刷入文件'''
 43 
 44     def isatty(self, *args, **kwargs): # real signature unknown
 45         pass '''判断当前文件是否连入tty设备'''
 46 
 47     def read(self, *args, **kwargs): # real signature unknown
 48         pass '''读取文件中所有内容'''
 49 
 50     def readable(self, *args, **kwargs): # real signature unknown  '''判断文件是否可读'''
 51         pass
 52 
 53     def readline(self, *args, **kwargs): # real signature unknown   '''仅读取文件的一行'''
 54         pass
 55 
 56     def seek(self, *args, **kwargs): # real signature unknown
 57         pass  '''设置文件指针的位置'''
 58 
 59     def seekable(self, *args, **kwargs): # real signature unknown   '''判断文件指针是否可修改'''
 60         pass
 61 
 62     def tell(self, *args, **kwargs): # real signature unknown
 63         pass '''返回该文件指针的当前位置'''
 64 
 65     def truncate(self, *args, **kwargs): # real signature unknown '''截断文件当前指针之后的内容,仅保留指针之前的'''
 66         pass
 67 
 68     def writable(self, *args, **kwargs): # real signature unknown '''判断文件是否可写'''
 69         pass
 70 
 71     def write(self, *args, **kwargs): # real signature unknown
 72         pass '''将字符串写入文件'''
 73 
 74     def __getstate__(self, *args, **kwargs): # real signature unknown
 75         pass
 76 
 77     def __init__(self, *args, **kwargs): # real signature unknown
 78         pass
 79 
 80     @staticmethod # known case of __new__
 81     def __new__(*args, **kwargs): # real signature unknown
 82         """ Create and return a new object.  See help(type) for accurate signature. """
 83         pass
 84 
 85     def __next__(self, *args, **kwargs): # real signature unknown
 86         """ Implement next(self). """
 87         pass
 88 
 89     def __repr__(self, *args, **kwargs): # real signature unknown
 90         """ Return repr(self). """
 91         pass
 92 
 93     buffer = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
 94 
 95     closed = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
 96 
 97     encoding = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
 98 
 99     errors = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
100 
101     line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
102 
103     name = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
104 
105     newlines = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
106 
107     _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
108 
109     _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
python3.5.1源码

 3.3 关闭文件

  这步操作并不是必须的,如果使用open()函数打开文件,那么要记得最终close()文件

如果使用with 语句打开的文件,则不需要。在with 语句主体语句执行完之后,会自动调用close()来关闭文件句柄。语法如下:

1 with open('db1', 'r', encoding="utf-8") as f1:
2     for line in f1:
3         print(line)

同时打开2个文件,语法如下:

1 with open('file1', 'r', encoding="utf-8") as f1, open("file2", 'w',encoding="utf-8") as f2:
2     pass

本章总结至此结束!

原文地址:https://www.cnblogs.com/jishuweiwang/p/5528905.html