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. 锋利的jQuery ——jQuery选择器(二)

    一.jQuery选择器 1)CSS选择器 CSS选择器有:1>标签选择器  E{CSS规则} 2>ID选择器   #ID{CSS规则} 3>类选择器  E.className{CSS ...

  2. element ui 选择期 传对象

    <template> <el-select value-key="label" v-model="value" placeholder=&qu ...

  3. Ruby下安装cocoapods

    常规安装:(文末:特殊安装) 注: 1.Mac OS X EI Capitan 10.11中需要更改安装路劲: sudo gem install -n /usr/local/bin cocoapods ...

  4. MyBatis中批量insert

    在orcale和mybatis执行批量插入是不一样的. orcale如下:(这里要注意的是:useGeneratedKeys="false" ) 方式1:oracle批量插入使用 ...

  5. 007-cobbler+koan自动化重装系统

    一.操作步骤 1.在使用cobbler自动化安装的虚拟机上做以下操作 2.安装epel源(先配置好yum) [root@localhost ~]# yum install epel-release - ...

  6. Schedule HDU - 6180 (multiset , 贪心)

    There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). Ther ...

  7. PHP简单的爬虫–原型

    1.PHP简单的爬虫–原型 爬虫的原理: 给定原始的url: 分析链接,根据设置的正则表达式获取链接中的内容: 有的会更新原始的url再进行分析链接,获取特定内容,周而复始. 将获取的内容保存在数据库 ...

  8. 组件通信 Provide&&inject

    在父组件中利用Provide 注入数据,在所有的子组件都可以拿到这个数据 可以在vue 中用来刷新页面 <!DOCTYPE html> <html lang="en&quo ...

  9. HTML5 入门基础

    HTML5概述HTML5於2004年被WHATWG(网页超文本技术工作小组)提出,於2007年被W3C接纳.在2008年1月22日,第一份正式草案已公布.WHATWG表示该规范是目前正在进行的工作,仍 ...

  10. VS插件CodeRush for Visual Studio发布v18.2.9|附下载

    CodeRush能帮助你以极高的效率创建和维护源代码.Consume-first 申明,强大的模板,智能的选择工具,智能代码分析和创新的导航以及一个无与伦比的重构集,在它们的帮助下能够大大的提高你效率 ...