当前位置: 移动技术网 > IT编程>脚本编程>Python > Python读书笔记:细节决定成败(2)

Python读书笔记:细节决定成败(2)

2019年04月30日  | 移动技术网IT编程  | 我要评论

背景:我本来是一个信奉java大法好的程序员。但是最近由于工作原因,不得不开始学习python。因此,写下这个笔记,希望能起到一个抛砖引玉的作用。原文中所有引用部分均来自python官方的tutorial

上接:python读书笔记:细节决定成败(1)

8.input() vs raw_input()

他们的作用都是用来读取命令行或者文件中的数据,区别在于返回结果

input()返回的是numeric,如int,flat,double

raw_input()返回的是string

举个例子:

>>> input()

12+2 #comand line input

14 #ouput

>>> raw_input()

12+2 #comand line input

'12+2' #ouput

然而在读过python的后,你会发现其实input()是通过raw_input来实现的:

def input(prompt):

return (eval(raw_input(prompt)))

9.output formating

从c语言开始,格式化字符串就已经为程序员所熟知,不管是c/c++还是java,我觉得都没有python在输出格式化方面做的这么简练易用。举两个例子:

zfill()

‘pads a numeric string on the left with zeros. it understands about plus and minus signs:‘

这个函数的厉害之处在于它不仅能够高位补零,而且可以识别正负号!

>>> '12'.zfill(5)

'00012'

>>> '-3.14'.zfill(7)

'-003.14'

str.format()

当有多个string需要输出时,这个函数非常的powerful:

>>> print 'we are the {} who say "{}!"'.format('knights', 'ni')

we are the knights who say "ni!"

with position:

>>> print '{0} and {1}'.format('spam', 'eggs')

spam and eggs

>>> print '{1} and {0}'.format('spam', 'eggs')

eggs and spam

with keyword:

>>> print 'this {food} is {adjective}.'.format(

... food='spam', adjective='absolutely horrible')

this spam is absolutely horrible.

combine position with keyword:

>>> print 'the story of {0}, {1}, and {other}.'.format('bill', 'manfred',

... other='georg')

the story of bill, manfred, and georg.

对于需要遍历输出对应项的情况,python更是给出了一个不错的解决方案—>:,再结合一下position和format,完美!

>>> table = {'sjoerd': 4127, 'jack': 4098, 'dcab': 7678}

>>> for name, phone in table.items():

... print '{0:10} ==> {1:10d}'.format(name, phone)

...

jack ==> 4098

dcab ==> 7678

sjoerd ==> 4127

10.serializing and deserializing

序列化(serializing)和反序列化(deserializing)是为了数据的易用性而出现的,在不同开发平台和互联网中,数据的表示方法一直都是处于百花齐放的局面。直到json的出现,才有了一个便于交换(interchange)的数据格式

序列化: 将python中的数据结构以字符串形式表示

反序列化: 将上述的字符串重建为python中的数据结构

json: javascript object notation

简单来说,只需要记住两个函数json.dumps()和json.load()就可以了。他们的函数原型分别是:

json.dumps(obj, skipkeys=false, ensure_ascii=true, check_circular=true, allow_nan=true, cls=none, indent=none, separators=none, encoding="utf-8", default=none, sort_keys=false, **kw)

两个函数的参数意义相同,可以自行tutorial了解详情,这里不展开了。

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

11.class objects

python是支持oop的语言,因此接下来几节介绍python中到的各种对象。

类是一个抽象的概念,类对象(class objects)支持两种操作(operations):

属性引用(attribute references)

实例化(instantiation)

与其它大多数oop语言一样,python的类也有变量和方法(method),不过却又略有不同。python的变量(data attribute)是属于类的,也即是说只要定义了类,那么不需要实例化,就能够直接访问,类似于java中类的静态变量(static variable).并且可以直接通过赋值(assign)去修改值。举例:

>>> class simple:

... i = 1

... def foo(self):

... return "hello world"

...

>>> simple.i

1

>>> simple.i = 2

>>> simple.i

2

但是类的方法却必须通过实例化后才能访问,python中的实例化也有其独特之处,实例对象(instance object)放在下节讲。如果没有实例化而直接去访问类中定义的方法,会报unbound method,这是因为方法(method)是必须作用到一个实际的对象上的,而类对象本身是抽象的:

>>> simple.foo

至于为什么类中的方法必须要自带一个self参数,我后面再讲,这也是python作为一个动态语言,与静态的oop之间最大的差异!

12.instance objects

实例对象(instance object)是一个具体的可操作的对象,其实例化的过程在python的官方解释中是这么说明的:

“class instantiation uses function notation. just pretend that the class object is a parameterless function that returns a new instance of the class.“

官方让我们假设类对象是返回一个实例对象的无参函数,所以实例化的过程就像是得到一个函数的返回结果。(晕吗?)个人感觉按照c++和java的构造器(constructor)来理解,也不会出现太大问题。

>>> x = simple()

>>> x.foo()

'hello world'

既然要实例化,必然躲不过一个词“初始化”(initialization),python中定义了__init()__这个方法来初始化实例对象。举个例子:

>>> class a:

... i = 1

... def __init__(self):

... self.i = 3

...

>>>

>>> a.i

1 #class attribute

>>> x = a()

>>> x.i

3 #instance attribute

>>> a.i

1 #class attribute

前面我做了比喻,类的变量和java中的静态变量类似,但这里可以看出它们的不同。在java中静态变量是属于类的,被其所有实例共用。然而python的变量不是共用的,所以我才说python的类理解起来更抽象一点,类也是一个对象,不过和实例对象却不同,更加抽象(晕吗?)

之所以有这么大的差异,主要原因是python是一个动态语言,它可以动态添加属性,这就令很多一直用java和c++的人不习惯。不过当你习惯之后,你会发觉它这种设计也真是好处多多。比如,你可以随时给你的类增加新的变量啊~

>>> class a:

... i = 1

...

>>> a.j = 2

>>> a.j

2

13.method objects

在python中,方法(method)和函数(function)是不同的。方法应该特指在类中定义的,其特征就是在定义时必须带上一个参数self。这和其他语言隐式的处理this这个参数不同(两者作用一样)。python必须要显示的指定这个方法作用的对象,这样的好处是在调用时能够确保方法是绑定(bound)在作用对象上的。因此,类是不能凭空调用方法的,必须作用在实例上才行,所以前面章节的例子里会出现unbound method这个错误提示。举个例子:

>>> class b:

... def f(self):

... return "hello method"

...

>>> b = b() #b is an instance object

>>> b.f()

'hello method'he

>>> b.f(b) #b is a class object

'hello method'

当类b有了实例b之后,它也是可以调用方法f的,因为这一切都只是为了确保这个方法的作用对象是存在的!现在回过头来看self,其实就是指类的实例本身作为参数传进这个函数:

“the special thing about methods is that the object is passed as the first argument of the function.“

这样也就好解释为什么函数本身定义时是可以不用显示的写self参数了,因为函数本身可以不属于任何类。如果有学过pascal这类面向过程的语言,就很好理解这句话了。

>>> def f(): #void, like a procedure in pascal

... 1 + 2

...

>>> f()

>>> def ff(): #return, like a function in pascal

... return 1 + 2

...

>>> ff()

3

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网