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. phpstorm配置成sublime的代码高亮逼格风格

    使用sublime text3的风格来编程感觉是相当逼格的,然而在php的时候还是觉得phpstorm用起来更顺手一点. 正好在phpstorm中也有配置sublime类似风格的插件,这里来教大家如何 ...

  2. Timer的利用

    package 第十一章; import java.util.*; import java.util.TimerTask; public class TimerTest { /** * @param ...

  3. [七月挑选]windows上面的发音

    title: windows上面的发音 开始 love.vbs: CreateObject("SAPI.SpVoice").Speak "I love YOU" ...

  4. octave - 用于数值计算的高级交互式语言

    SYNOPSIS 总览 octave [options] OPTIONS 选项 octave 全部命令行选项可以通过运行命令 octave --help 来查看. DESCRIPTION 描述 Oct ...

  5. 04-A的LU分解

    一.矩阵$AB$的逆 $(AB)^{-1}=B^{-1}A^{-1}$,顺序正好相反 二.$A=LU$ 如矩阵: $\left[\begin{array}{ll}{2} & {1} \\ {8 ...

  6. html常见标签及用法整理

    <!DOCTYPE html> <!--#浏览器的兼容模式--> <html lang="en"> <head> <!--he ...

  7. springboot之学习搭建

    什么是**SpringBoot?** Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配 ...

  8. python 删除/app/*/logs/*/*.logs指定多少天的文件

    # encoding: utf-8 import sys import getopt import os import glob import time import datetime def rem ...

  9. for循环性能测试

    项目中遇到需要用for循环dom节点获取每个节点保存的数据,一共有8000多条数据,但是循环加载需要18秒左右才出来. 平时for循环中数据少感觉不出来,现在数据多了就有差别了. 部分代码如下 var ...

  10. C#中[JsonIgnore]意义

    字面意义是忽略序列化,就是当字段在序列化时,被[JsonIgnore]标记了的字段将被忽略序列化 序列化输出中使用Id和Name属性,但我绝对不会对AlternateName和Color感兴趣.我用[ ...