Coursera课程《Using Databases with Python》 密歇根大学 Charles Severance

Week1 Object Oriented Python

Unicode Characters and Strings

每个字符都被数字0到256之间的数字所表示,以此来存储在8比特的内存里。这个码我们成为ASCII码。

下表来自ASCII码对照表

Multi-Byte Characters

为了显示更广范围的字符,电脑不得不处理大于1byte的字符。

  • UTF-16 2bytes
  • UTF-32 4bytes
  • UTF-8 1-4bytes

重点说下UTF-8编码方式现在非常流行,我们在代码里输入输出中文、日文等字符都要使用这种编码方式。所以不管是要跨操作系统还是跨什么什么,都强推使用UTF-8的编码方式。

在Python3,所有的字符串都是Unicode。

Python Strings to Bytes

while True:
data = mysock.recv(512) #这里是bytes
if (len(data) < 1):
break
mystring = data.decode() #这里变成了unicode
print(mystring)

An HTTP Request in Python

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\n\n'.encode() # 转换成bytes
mysock.send(cmd) while True:
data = mysock.recv(512)
if (len(data) < 1):
break
print(data.decode())
mysock.close()

也就是下图这个原理。

14.1 Object Oriented Definitions and Terminology

注意:类和实例的区别,其实就相当于人类与个体的区别。

14.2 Our First Class and Object

所以我们创建我们第一个类PartyAnimal,代码就是下图框框里的样子。

第一行申明这个类,接下来的黄色和绿色的缩进部分就是这个类的内容。黄色部分是这个类所包含的数据,绿色部分则是这个类的方法。到此为止,计算机还没做什么实质性的工作。

橙色部分开始创建了一个PartyAnimal的实例,也就类似于x=list()是创建了list的一个实例。而洋红色部分则是这个实例使用了这个类的方法,也就类似于x.sort(),x这个list使用了sort这个方法。

Playing with dir() and type()

dir()这个方法可以列出当前实例可使用的方法,但是我们可以忽略那些带下划线的方法,这些方法是python自带的自用的。而剩下的那些方法则是这个实例可以具体操作的。

>>> y = list()
>>> type(y)
<type 'list'>
>>> dir(x)
['__add__','__class__','__contains__','__delattr__','__delitem__','__delslice__','__doc__',...'__setitem__','__setslice__','__str__','append','clear','copy','count','extend','index','insert','pop','remove','reverse','sort']

type()方法则可以让我们查看当前实例属于的类。

14.3 Object Life Cycle

Constructor

class PartyAnimal:
x = 0 def __init__(self):
print('I am constructed') def party(self):
self.x = self.x + 1
print('So far', self.x) def __del__(self):
print('I am destructed', self.x) an = PartyAnimal()
an.party()
an.party()
an = 42
print('an contains', an)

输出结果

I am constructed
So far 1
So far 2
I am destructed 2
an contains 42

这个constructor和destructor是可选的参数,也就是代码中的__init__和_del_。constructor一般用于初始化变量,destructor一般很少使用。

注意上面的代码,在第17行的时候,an赋值给了整数42,也就是变成了integer的class了。

class PartyAnimal:
x = 0
name = ""
def __init__(self, z):
self.name = z
print(self.name,"constructed") def party(self):
self.x = self.x + 1
print(self.name, "party count", self.x) s = PartyAnimal("Sally")
s.party() j = PartyAnimal("Jim")
j.party()
s.party()

constructors可以有额外的参数,这可以方便设置初始值。

14.4 Object Inheritance

Inheritance

当我们有一个新的类,我们可以再利用一个已经存在的类,然后继承它的所有capabilities,还可以额外再加点新的东西。这样,被继承的类称为父类,继承的类称为子类

class PartyAnimal:
x = 0
name = ""
def __init__(self, nam):
self.name = nam
print(self.name,"constructed") def party(self):
self.x = self.x + 1
print(self.name,"party count", self.x) class FootballFan(PartyAnimal):
points = 0
def touchdown(self):
self.points = self.points + 7
self.party()
print(self.name,"points",self.points) s = PartyAnimal("Sally")
s.party() j = FootballFan("Jim")
j.party()
j.touchdown()

上面这个例子,FootballFan就是PartyAnimal的一个子类。

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记的更多相关文章

  1. 潭州课堂25班:Ph201805201 爬虫基础 第七课 Python与常见加密方式 (课堂笔记)

    打开图形界面  18版 Python与常见加密方式 前言 我们所说的加密方式,都是对二进制编码的格式进行加密的,对应到Python中,则是我们的Bytes. 所以当我们在Python中进行加密操作的时 ...

  2. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  3. Python学习札记(三十) 面向对象编程 Object Oriented Program 1

    参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...

  4. PSPInstance Object | Web Python

    PSPInstance Object | Web Python The PSPInstance object is available to all PSP pages through the psp ...

  5. Python – Get Object’s Class Name | Ridge Solutions, Ireland

    Python – Get Object’s Class Name | Ridge Solutions, Ireland Python – Get Object’s Class Name Author: ...

  6. appium 与 selenium python解决python 'WebElement' object does not support indexing 报错问题问题

    再用selenium编写测试脚本时,发现出现python 'WebElement' object does not support indexing 报错问题问题,再找一些解决方法时,发现Appium ...

  7. 【转】实习小记-python中可哈希对象是个啥?what is hashable object in python?

    [转]实习小记-python中可哈希对象是个啥?what is hashable object in python? 废话不多说直接祭上python3.3x的文档:(原文链接) object.__ha ...

  8. How to convert a QString to unicode object in python 2?

    How to convert a QString to unicode object in python 2? I had this problem to solve, and I tried to ...

  9. python's object model

    [python's object model] 1.object.__init__(self[, ...])        如果subclass没有实现__init__,那么python类在实例化的时 ...

随机推荐

  1. python-xss攻击和单例模式

    1.python中单例模式 class Foo: instance = None def __new__(cls, *args, **kwargs): if not Foo.instance: Foo ...

  2. ES6中class的实现原理

    一.在ES6以前实现类和继承 实现类的代码如下: function Person(name, age) { this.name = name; this.age = age; } Person.pro ...

  3. 学习R的悬疑录(不定期更新)

    在使用caret包建模时候,没有导入机器学习包,如rpart.randomForest等,并不会对结果有影响.不知道是不是caret包内置了机器学习模块. # 加载r包 library(data.ta ...

  4. PAT Advanced 1027 Colors in Mars (20 分)

    People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...

  5. Codeforces 960 二进制构造子序列 完全二叉树shift模拟 主席树/MAP DP

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  6. c调用c++

    g++ main.c math.cpp math.h中加入extern "C"

  7. Linux中查看某 个软件的安装路径

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...

  8. 如何将上个SQL的结果作为参数传递给下个SQL

    如何将上个SQL的结果作为参数传递给下个SQL: ##source认证 kinit认证 source /home/omm/ficlient/bigdata_env kinit -k -t /ETL/c ...

  9. 【leetcode】Decode Ways

    题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...

  10. 【leetcode】493. Reverse Pairs

    题目如下: 解题思路:本题要求的是数组每个元素和所有排在这个元素后面的元素的值的二倍做比较.我们可以先把数组所有元素的二倍都算出来,存入一个新的数组newlist,并按升序排好.而后遍历nums数组的 ...