最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】Python经典教程《Dive Into Python》的学习笔记

Python crifan 2044浏览 0评论

之前虽然折腾了一些Python代码:crifanLib.pyBlogsToWordpressrecSongtasteMusic,但是,也都是边写边学,没有系统学习过Python。

所以现在打算抽空去完整的学习一下Python。

选用的教程是比较经典的,之前就听过大名的《Dive Into Python》,参考官网中的中文版,在线阅读:

深入 Python :Dive Into Python 中文版

下面就记录一些学习心得。


1.DocString/docstring/ doc string

之前还真不知道有这个东东的。

举例:

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters.

    Returns string."""

看来以后写代码,要尽量养成写doc string的习惯了。

再引用PEP 257 — Docstring Conventions中的一句解释吧:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

 

后来也参考:万物皆对象去打开IDLE中,去试着打印__doc__:

>>> import math
>>> print math.__doc__
This module is always available.  It provides access to the
mathematical functions defined by the C standard.

 

2.关于sys.path

之前就在,想要导入自己的库函数crifanLib.py时,而折腾过,现在也在此看到了具体的解释,通过

sys.path.append(‘/my/new/path’);

的方法添加新路径,使得可以找到你自己的导入的库。

 

3.关于万物皆对象

其中参考:Python Objects得知,以后除了使用type打印对象的类型之外,还可以通过id(xxx)获得其id的值。

不同的对象的id值是唯一的。

 

4.Dict

(1)Dictionary的key的大小写是敏感的

这点,之前就知道,且也容易理解。只是此处再次提醒一下。

因为自己之前写程序时,就不小心遇到变态的csdn的帖子的url中,对于用户名,有的是大写的,有的是小写的。

导致以url为key去存取值时,偶尔遇到了url中的用户名是大写的,导致无法获取之前存入的值。

所以,以后在这方面要小心。

(2)用 del dictValue[key]去删除dictValue中的key;

用dictValue.clear()可以清除所有的键值。

转载请注明:在路上 » 【整理】Python经典教程《Dive Into Python》的学习笔记

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.314 seconds, using 22.14MB memory