《Dive into python3》第一天

第一个程序

关键词: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']

① 一旦你引入一个模块,你便可以引用这个模块中任何其他的公共函数、类或属性等

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

③不是所有的模块都是以.py文件形式存储的,有些属于内置模块,如sys;它们被烧进至python。内置模块表现和普通函数一样,  但是他们的python源代码是不可用的,因为他们不是用python写的(就像python本身,这些内置模块是用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,诸如此类。在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 python中的缩进

 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中使用句式:try...except...来处理异常,raise语句捕获异常;

在Java中则是;try...catch...,用throw来抛出异常。


1.8 未绑定变量(变量命名区分大小写)

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

 

1.9 运行脚本

python脚本的运行可以使用F5键,也可以使用dos命令行,或者Pycharm等工具。

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

第一章完

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值