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

Week1 Object Oriented Python

Unicode Characters and Strings

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

下表来自ASCII码对照表

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

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()

也就是下图这个原理。

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

14.1 Object Oriented Definitions and Terminology

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

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

14.2 Our First Class and Object

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

《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

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

橙色部分开始创建了一个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的一个子类。

相关文章:

  • 2021-06-30
  • 2019-08-18
  • 2022-12-23
  • 2021-08-22
  • 2022-02-19
  • 2021-08-13
  • 2022-03-02
  • 2021-04-28
猜你喜欢
  • 2021-07-29
  • 2021-12-06
  • 2021-11-05
  • 2021-08-30
  • 2021-04-04
  • 2021-12-25
相关资源
相似解决方案