输入输出

  • str() 函数人类可读,repr() 解释器可读
  • rjust() 靠右,ljust() 靠左,center() 居中。zfill() 填0。

名字空间

  • 名字到对象的映射关系被称为名字空间。
  • 名字空间采用字典来实现。
  • 不同名字空间中相同的名字各不相关。
  • 存在以下名字空间(按照搜索顺序):
    • 最内层函数的局部名字
    • 外层函数的非全局非局部名字
    • 模块的全局名字
    • 语言的内置名字
def scope_test():
def do_local():
spam = "local spam" def do_nonlocal():
nonlocal spam
spam = "nonlocal spam" def do_global():
global spam
spam = "global spam" spam = "test spam"
do_local()
print("After local assignment:", spam)
do_nonlocal()
print("After nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam) scope_test()
print("In global scope:", spam)
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam

类(Classes)

  • 类的成员缺省为public,方法在子类中均可覆盖(重写)。
  • 类名自身是一个对象。
  • 类的方法的第一个参数通常是指向类实例自身的 self 参数。
  • 方法名本身是一个对象。
  • 存在类变量和实例变量。
  • 至少两个下划线打头的变量为private变量。
  • 内置类型也可以作为基类。
  • 存在多重继承。
>>> class MyClass:
"""A simple example class"""
i = 12345 def __init__(self):
self.data = [] def f(self):
return 'hello world' >>> MyClass.i
12345
>>> MyClass.f(None)
'hello world'
>>> MyClass.__doc__
'A simple example class'
>>> x = MyClass()
>>> x.i
12345
>>> x.f()
'hello world'
>>> xf = x.f
>>> print(xf())
hello world
>>> class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart >>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)

数据属性(data attributes)和方法(methods)

  • 类成员包括数据属性和方法。

    上面的例子中

    MyClass 是类,MyClass.i 是类对象的数据属性,MyClass.f 是函数对象(function object)。

    x 是类的实例,x.data 是实例的数据属性,x.f 是方法对象(method object)。
  • 方法的第一个(隐藏)参数是类的实例对象。

    x.f() 实际相当于 f(x)。
  • 实例对象的属性和方法为实例对象所有,而类对象的属性何方法为类的所有实例对象所共有。
>>> class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
self.name = name # instance variable unique to each instance >>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind # shared by all dogs
'canine'
>>> e.kind # shared by all dogs
'canine'
>>> d.name # unique to d
'Fido'
>>> e.name # unique to e
'Buddy'
>>> class Dog: def __init__(self, name):
self.name = name
self.tricks = [] # creates a new empty list for each dog def add_trick(self, trick):
self.tricks.append(trick) >>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks
['roll over']
>>> e.tricks
['play dead']
  • 属性名和方法名冲突时,属性名优先。
  • 实例对象可以在类之外添加自己独有的属性。
  • 方法可以在类之外定义。
# Function defined outside the class
def f1(self, x, y):
return min(x, x+y) class C:
f = f1 def g(self):
return 'hello world' h = g
  • 方法可以调用其他方法。
class Bag:
def __init__(self):
self.data = [] def add(self, x):
self.data.append(x) def addtwice(self, x):
self.add(x)
self.add(x)
  • 实例对象可以引用自己的类对象 object.class
  • 空的类定义可以用来模拟其他语言中的记录和结构。
class Employee:
pass john = Employee() # Create an empty employee record # Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

继承

  • 继承的语法为 class DerivedClassName(modname.BaseClassName):
  • 基类中的方法可以在子类中被覆盖(重写)。
  • 子类可以通过BaseClassName.methodname(self, arguments)调用基类中的(同名)方法。
  • isinstance() 检查是否类的实例

    如果 obj 是 int 类型或其子类的实例的话,那么 isinstance(obj, int) == True 成立。
  • issubclass() 检查是否类的子类

    issubclass(bool, int) == True

    issubclass(float, int) == False
  • 多重继承的语法为class DerivedClassName(Base1, Base2, Base3):

迭代器

迭代器用例

>>> for element in [1, 2, 3]:
print(element) 1
2
3
>>> for element in (1, 2, 3):
print(element) 1
2
3
>>> for key in {'one':1, 'two':2}:
print(key) one
two
>>> for char in "123":
print(char) 1
2
3
>>> for line in open("myfile.txt"):
print(line, end='')
  • for 循环能够迭代容器 container,是因为容器 container 实现了迭代器的接口。
  • for 循环首先调用 iter(container) 得到迭代器对象 iterator。
  • for 循环反复调用 next(iterator) 得到容器 container 中的元素。
  • 当容器中的元素被迭代完毕时,next(iterator) 抛出 StopIteration 异常,for 循环终止。

迭代器细节

>>> s = 'abc'
>>> it = iter(s)
>>> it
<str_iterator object at 0x02C95530>
>>> next(it)
'a'
>>> next(it)
'b'
>>> next(it)
'c'
>>> next(it)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
next(it)
StopIteration
  • iter(container) 调用 container 的 _iter_ 方法。

    该方法返回 iterator 对象。
  • next(iterator) 调用 iterator 的 _next_ 方法。

    该方法返回下一个元素或者抛出 StopIteration 异常。
  • 通常 container 和 iterator 是同一个对象。

迭代器示例

>>> class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data) def __iter__(self):
return self def __next__(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index] >>> rev = Reverse('spam')
>>> iter(rev)
<__main__.Reverse object at 0x02C95670>
>>> for char in rev:
print(char) m
a
p
s

生成器

生成器函数使用 yield 自动实现迭代器接口。

>>> def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index] >>> for char in reverse('golf'):
print(char) f
l
o
g

生成器表达式

生成器表达式使用小括号加解析表达式自动生成迭代器接口。

>>> sum(i*i for i in range(10))                 # sum of squares
285 >>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec)) # dot product
260 >>> from math import pi, sin
>>> sine_table = {x: sin(x*pi/180) for x in range(0, 91)} >>> unique_words = set(word for line in page for word in line.split()) >>> valedictorian = max((student.gpa, student.name) for student in graduates) >>> data = 'golf'
>>> list(data[i] for i in range(len(data)-1, -1, -1))
['f', 'l', 'o', 'g']

Python 3 学习笔记(2)的更多相关文章

  1. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL

    周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...

  2. Python Click 学习笔记(转)

    原文链接:Python Click 学习笔记 Click 是 Flask 的团队 pallets 开发的优秀开源项目,它为命令行工具的开发封装了大量方法,使开发者只需要专注于功能实现.恰好我最近在开发 ...

  3. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  4. Python Flask学习笔记之模板

    Python Flask学习笔记之模板 Jinja2模板引擎 默认情况下,Flask在程序文件夹中的templates子文件夹中寻找模板.Flask提供的render_template函数把Jinja ...

  5. Python Flask学习笔记之Hello World

    Python Flask学习笔记之Hello World 安装virtualenv,配置Flask开发环境 virtualenv 虚拟环境是Python解释器的一个私有副本,在这个环境中可以安装私有包 ...

  6. 获取字段唯一值工具- -ArcPy和Python案例学习笔记

    获取字段唯一值工具- -ArcPy和Python案例学习笔记   目的:获取某一字段的唯一值,可以作为工具使用,也可以作为函数调用 联系方式:谢老师,135-4855-4328,xiexiaokui# ...

  7. Python高级学习笔记

    Python高级学习笔记,此笔记中包含Linux操作系统.Html+CSS+JS.网络协议等. 所有思维导图为本人亲手所画,请勿用于商用. 大哥们,求点赞哦. 第一天笔记:链接 第二天笔记:链接 第三 ...

  8. Python入门学习笔记4:他人的博客及他人的学习思路

    看其他人的学习笔记,可以保证自己不走弯路.并且一举两得,即学知识又学方法! 廖雪峰:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958 ...

  9. Python 基础学习笔记(超详细版)

    1.变量 python中变量很简单,不需要指定数据类型,直接使用等号定义就好.python变量里面存的是内存地址,也就是这个值存在内存里面的哪个地方,如果再把这个变量赋值给另一个变量,新的变量通过之前 ...

  10. Python人工智能学习笔记

    Python教程 Python 教程 Python 简介 Python 环境搭建 Python 中文编码 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 ...

随机推荐

  1. JSON.stringify、JSON.parse、toJSON 区别

    JSON.stringify 方法 将一个 JavaScript 值转换为一个 JSON 字符串 可以将数组.对象等转换后的 JSON 字符串,保存在 sessionStorage.localStor ...

  2. R(7): data.table

    这个包让你可以更快地完成数据集的数据处理工作.放弃选取行或列子集的传统方法,用这个包进行数据处理.用最少的代码,你可以做最多的事.相比使用data.frame,data.table可以帮助你减少运算时 ...

  3. 重置SQL Server sa密码

    查询分析器,连接时,身份验证使用"使用windows身份验证" 然后,执行: EXEC sp_password NULL, '新密码', 'Sa'

  4. lwip调试记录

    1. lwip在调用tcp_write后不会立即发送数据,而会等到tcp_slow_tmr中再发送.如需立即发送,可以在tcp_write后调用tcp_output.lwip的这种处理方式对连续调用t ...

  5. bzoj1050 旅行

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

  6. DIV+CSS如何让文字垂直居中?(转)

    此篇文章转自网络,但是我忘了原文地址,如果有人知道,麻烦告知一声~ 在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少 ...

  7. 1067 Sort with Swap(0, i) (25 分)

    1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...

  8. tp5的RBAC插件及其使用很方便的管理用户登录及操作权限

    tp5-rbac 本扩展包是tp5的rbac包,使用了部分tp5的特性实现了关系型数据库中特殊数据结构的处理. 安装方法 先安装composer如果不知道怎么安装使用composer请自行百度. 打开 ...

  9. linux下一个监测进程CPU和MEM使用率的shell脚本

    #!/bin/bashPID=$1 cpu=`ps --no-heading --pid=$PID -o pcpu`mem=`ps --no-heading --pid=$PID -o pmem`ec ...

  10. 基于标准库实现string和wstring的转换

    // convert string to wstring std::wstring to_wstring(const std::string& str, const std::locale&a ...