《Dive into python3》第一天

The first program

关键词:Docstring; import search path; object; Indenting code; exception.

1.4  The import search path

>>> import sys                                           '①'
>>> sys.path                                             '②'
['', 'F:\\python3\\Lib\\idlelib', 'F:\\python3\\python37.zip', 'F:\\python3\\DLLs', 'F:\\python3\\lib', 'F:\\python3', 'F:\\python3\\lib\\site-packages']
>>> sys                                                  '③'
<module 'sys' (built-in)>
>>> sys.path.insert(0, '/python3/wq/diveintopython3/examples')  '④'
>>> sys.path
['/python3/wq/diveintopython3/examples', '', 'F:\\python3\\Lib\\idlelib', 'F:\\python3\\python37.zip', 'F:\\python3\\DLLs', 'F:\\python3\\lib', 'F:\\python3', 'F:\\python3\\lib\\site-packages']

>>> help(list)                                                  '⑤'

。。。。。。

remove(self, value, /)
 |      Remove first occurrence of value.
 |      
 |      Raises ValueError if the value is not present.

。。。。。。

>>> sys.path.remove('/python3/wq/diveintopython3/examples')    '⑥'
>>> sys.path
['', 'F:\\python3\\Lib\\idlelib', 'F:\\python3\\python37.zip', 'F:\\python3\\DLLs', 'F:\\python3\\lib', 'F:\\python3', 'F:\\python3\\lib\\site-packages']

① Once you import a module, you can reference any of its public functions, classes, or attributes.

② 调用当前搜索路径的目录名列表

③原文:not all modules are stored as .py files. Some are built-in modules; they are actually baked right into Python itself. Built-in modules behave just like regular modules, but their Python source code is not available, because they are not written in Python! (Like Python itself, these built-in modules are written in C.)

不是所有的模块都是以.py文件形式存储的,有些属于内置模块,如sys;它们被烧进至python。内置模块表现和普通函数一样,但是他们的python源代码是不可用的,因为他们不是用python写的(就像python本身,这些ne是用内置模块是用C实现的)

④加入一条路径名

⑤help() or help(object)与python的交互,作用和linux里的man相同,当我们想要知道某个函数怎么用可以使用help。例如上面的标号⑤:我想删掉我刚刚插入的那条路径,但我不知道用什么函数或某个属性可以,那么我可以使用help(list),列举了所有的属性,我从中找到了remove()

⑥移除一条搜索路径名:remove()的实际使用。


1.5 一切皆对象

>>> import humansize                               '①将humansize.py作为一个模块引入'
>>> print(humansize.approximate_size(4096,True))   '②调用模块里的函数'
4.0 KiB
>>> print(humansize.approximate_size.__doc__)      '③打印出这个函数的doc string(函数的属性之一,解释这个函数的定义、作用等'
Convert a file size to human-readable form.

    Keyword arguments:
    size -- file size in bytes
    a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
                                if False, use multiples of 1000

    Returns: string

1.5.1 什么是对象

python中的一切事物都是一个对象,每件事物都可以有属性和方法,所有的函数都有一个内置属性__doc__。sys模块是一个对象,他有一个属性path。诸如此类。

In Python, the definition is looser. Some objects have neither attributes nor methods, but they could. Not all objects are subclassable. But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function.

译:在python中,对对象的定义很宽松。有些对象既没有属性也没有方法,但他们可以是对象。不是所有的对象都要求可子类化。从某种意义上来说,所有的东西都是一个对象它可以被分配给一个变量或者作为一个参数传递给一个函数。

This is important, so I’m going to repeat it in case you missed it the first few times: everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Classes are objects. Class instances are objects. Even modules are objects.


1.6 Indenting code

 Indenting starts a block and unindenting ends it. This means that whitespace is significant, and must be consistent.

(缩进开始一个块,而不缩进则结束它。这意味着空格是重要的,并且必须是一致的。)

ps:Python uses carriage returns to separate statements and a colon and indentation to separate code blocks. c++ and Java use semicolons to separate statements and curly braces to separate code blocks.(Python使用回车来分隔语句和冒号和缩进来分隔代码块。c++和Java使用分号来分隔语句和花括号来分隔代码块)


1.7 异常

☞Python uses try...except blocks to handle exceptions, and the raise statement to generate them. Java and c++ use try...catch blocks to handle exceptions, and the throw statement to generate them.

1.7.1 CATCHING IMPORT ERROR

try...except...的使用


1.8 未绑定变量

在python中,允许不定义变量,但必须给变量赋值


1.9 所有的都区分大小写

1.10 运行脚本

Python模块是对象,并且有几个有用的属性。您可以使用它来轻松地测试您的模块,包括在命令行上运行Python文件时执行的特殊代码块。例如humansize.py最后三行的作用是测试自己所写的模块:

ps: Like c, Python uses == for comparison and = for assignment. Unlike c, Python does not support in-line assignment, so there’s no chance of accidentally assigning the value you thought you were comparing.

内联函数的主要作用http://www.elecfans.com/emb/604060.html

第一章完

猜你喜欢

转载自blog.csdn.net/qq_38576427/article/details/81626348
今日推荐