牛骨文教育服务平台(让学习变的简单)

前面已经洋洋洒洒地介绍了不少数据类型。不能再不顾一切地向前冲了,应当总结一下。这样让看官能够从总体上对这些数据类型有所了解,如果能够有一览众山小的感觉,就太好了。

下面的表格中列出了已经学习过的数据类型,也是python的核心数据类型之一部分,这些都被称之为内置对象。

对象,就是你面对的所有东西都是对象,看官要逐渐熟悉这个称呼。所有的数据类型,就是一种对象。英文单词是object,直接的汉语意思是物体,这就好像我们在现实中一样,把很多我们看到和用到的都可以统称为“东西”一样。“东西”就是“对象”,就是object。在编程中,那个所谓面向对象,也可以说成“面向东西”,是吗?容易有歧义吧。

对象类型 举例
int/float 123, 3.14
str "qiwsir.github.io"
list [1, [2, "three"], 4]
dict {"name":"qiwsir","lang":"python"}
tuple (1, 2, "three")
set set("qi"), {"q", "i"}

不论任何类型的数据,只要动用dir(object)或者help(obj)就能够在交互模式下查看到有关的函数,也就是这样能够查看相关帮助文档了。举例:

>>> dir(dict)

看官需要移动鼠标,就能够看全(下面的本质上就是一个list):

["__class__", "__cmp__", "__contains__", "__delattr__", "__delitem__", "__doc__", "__eq__", "__format__", "__ge__", "__getattribute__", "__getitem__", "__gt__", "__hash__", "__init__", "__iter__", "__le__", "__len__", "__lt__", "__ne__", "__new__", "__reduce__", "__reduce_ex__", "__repr__", "__setattr__", "__setitem__", "__sizeof__", "__str__", "__subclasshook__", "clear", "copy", "fromkeys", "get", "has_key", "items", "iteritems", "iterkeys", "itervalues", "keys", "pop", "popitem", "setdefault", "update", "values", "viewitems", "viewkeys", "viewvalues"]

先略过__双下划线开头的哪些,看后面的,就是dict的内置函数。至于详细的操作方法,通过类似help(dict.pop)的方式获得。这是前面说过的,再说一遍,加深印象。

我的观点:学习,重要的是学习方法,不是按部就班的敲代码。

今天既然是复习,就要在原来基础上提高一点。所以,也要看看上面那些以双下划线开头的东西,请看官找一下,有没有发现这个:"doc__"。这是什么,它是一个文件,里面记录了对当前所查看的对象的详细解释。可以在交互模式下这样查看:

>>> dict.__doc__

显示应该是这样的:

"dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object"s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)"

注意看上面乱七八糟的英文中,是不是有 符号,这是什么?前面在讲述字符串的时候提到了转义符号,这是换一行。也就是说,如果上面的文字,按照排版要求,应该是这样的(当然,在文本中,如果打开,其实就是排好版的样子)。

"dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object"s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)"

可能排版还是不符合愿意。不过,看官也大概能看明白了。我要说的不是排版,要说的是告诉看官一种查看某个数据类型含义的方法,就是通过obj.doc文件来看。

嘿嘿,其实有一种方法,可以看到排版的结果的:

>>> print dict.__doc__
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object"s
    (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
        d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list.  For example:  dict(one=1, two=2)

上面那么折腾一下,就是为了凑篇幅,不然这个总结的东西太少了。

总之,只要用这种方法,你就能得到所有帮助文档,随时随地。如果可以上网,到官方网站,是另外一种方法。

还需要再解释别的吗?都多余了。唯一需要的是看官要能会点英语。不过我相信看官能够读懂,我这个二把刀都不如的英语水平,还能凑合看呢,何况看官呢?

总结不是意味着结束,是意味着继往开来。精彩还在后面,这里只是休息。今天还是周日。

主日崇拜

腓立比書 Philippians(3:13-14)

Brethren, I count not myself to have apprehended: but this one thing I do, forgetting those things which are behind, and reaching forth unto those things which are before, I press toward the mark for the prize of the high calling of God in Christ Jesus.

弟兄們、我不是以為自己已經得著了.我只有一件事、就是忘記背後努力面前的, 向著標竿直跑、要得神在基督耶穌裡從上面召我來得的獎賞 。

忘记背后,努力面前,向着标杆直跑