Python 面向对象进阶

1 isinstance 和issubclass

instance:判断该对象是否是类的对象

isinstance(obj,Foo)

x = []
print(isinstance(x,list))

结果:
True

issubclass:判断是否是继承


class Foo:
    pass
class Bar(Foo):
    pass
print(issubclass(Bar,Foo))

结果:
True

通过下面的方式也可以查看
print(Bar.base)

2 反射

反射是Smi首次提出的,主要指程序可以访问、检测和修改它本身状态行为的一种能力,也可以叫做自省

2.1面向对象中的反射

面向对象中的反射,是通过字符串形式操作对象的属性,Python中一切都是对象,都可以使用对象

Python中通过hasattr、getattr、setattr、deltattr实现自省的函数,适用于类和对象(一切都是对象)

反射就是把字符串反射成相应的命令

hasattr

查看类和对象中是否有name,用字符串表示,实例在找的时候首先从自身找,自身没有从类找。

class Foo:
    name = ‘aaa‘
f1 =Foo()
print(hasattr(Foo,‘name‘))
print(hasattr(f1,‘name‘))

结果:
True
True

setattr

setattr是设置属性,setattr(x,y,z)

class Foo:
    name = ‘aaa‘
f1 =Foo()

setattr(Foo,‘age‘,18)
print(Foo,‘age‘)
print(f1,‘age‘)

deltattr

print(Foo.__dict__)
delattr(Foo,‘name‘)
print(Foo.__dict__)
print(hasattr(f1,‘name‘))

结果:
False
通过打印类的名称空间可以查看到name已经没有了

getattr

getattr是查找那个属性,并把命名它的值获得,实际的原理是通过获得字典中的key,然后获得value

class People:
    country = ‘china‘
    def __init__(self,name):
        self.name=name

p = People(‘aaa‘)
print(hasattr(p,‘name‘))
print(‘name‘ in p.__dict__)  # 执行的效果是一样的e

print(hasattr(p,‘country‘))
print(hasattr(People,‘country‘))

print(getattr(p,‘country‘))
print(getattr(p,‘__init__‘))
print(getattr(People,‘country‘))

2.3模块的反射

Python中一切皆对象,模块文件等都是对象

import sys

def s1():
    pass
def s2():
    pass
this_module = sys.modules[__name__]
print(hasattr(this_module,‘s1‘))
print(getattr(this_module,‘s1‘))
  • 结果:
    True

function s1 at 0x00000000005F3E18

3 内置attr

getattr

getattr只有在调用的属性不存在的时候才会触发

class Foo:
    def __init__(self,name):
        self.name = name
    def __getattr__(self, item):
        print("getattr--%s %s"%(item,type(item)))

f = Foo(‘aaa‘)
print(f.xxx)
  • 结果:
    getattr—xxx class ‘str’
    None

setattr

setattr添加/修改属性会触发它的执行

class Foo:
    def __init__(self,x):
        self.name=x
    def __setattr__(self, key, value):  # 与上面的对应关系是self--self,name--key,x--value
        self.__dict__[key]=value  # 真正修改的是__dict__中的内容
f1 = Foo(‘aaa‘)
f1.x=‘bbb‘   # 修改属性
f1.age=18  # 增加属性
print(f1.__dict__) # 在名称对象的名称空间中可以显示

deltattr

只有删除属性的时候才会触发deltatr的执行

class Foo:
    def __init__(self,x):
        self.name=x
    def __setattr__(self, key, value):  # 与上面的对应关系是self--self,name--key,x--value
        self.__dict__[key]=value  # 真正修改的是__dict__中的内容
    def __delattr__(self, item):
        self.__dict__.pop(item)
f1=Foo(‘aaa‘)
print(f1.__dict__)
del f1.name
print(f1.__dict__)

结果:
{‘name’: ‘aaa’}
{}

4 反射的应用

4.1 可插拔机制

#客户端
class FtpClient:
    def __init__(self,addr):
        print(‘正在连接服务器[%s]‘%addr)
        self.addr = addr

    def get(self):
        print(‘get‘)

    def test(self):
        print(‘test‘)
#服务端,需要使用客户端的内容
import ftpclient  # 导入test模块(这里是文件)
# f1 = FtpClient(‘192.168.1.1‘)  # wrong
f1=ftpclient.FtpClient(‘192.168.1.1‘)   #这里的对象是文件,即ftpclient,对这个对象进行实例化
if hasattr(f1,‘get‘):  # 首先看是否有这个功能
    func_get=getattr(f1,‘get‘)  # getattr是获得相应的内存地址
    func_get()
else:  # else预语言中是真正的逻辑
    print(‘其他逻辑‘)

4.2 通过字符串导入模块

m = input("输入你要导入的模块")
m1 = __import__(m)
print(m1)
import importlib
t = importlib.import_module(‘time‘)
print(t.time)

5 二次加工标准类型(包装)

Python内部已经有了标准的数据类型和内置方法,但是在很多的情况下,我们需要对数据类型进行自己定制,这几用到了继承和派生的知识

对append添加限制,只能添加数字类型的

class List(list): # List 继承了列表list
    def append(self, p_object):  # 仅仅对append进行修改
        if not isinstance(p_object,int):  # 限制数据类型
            raise TypeError(‘must be int‘)
        super().append(p_object)  # 真正的修改,super是对父类进行修改
l = list([1,2,3])
print(l)
l.append(4)   # 这里是对属性进行修改,实际是对类进行修改
print(l)

结果:
[1, 2, 3]
[1, 2, 3, 4]

上面的情况仅仅是对append进行了修改并添加了限制,list的其他的属性如insert是没有限制的

授权

授权是包装的一个特性,上面的情况是通过继承来实现的,但是函数是不能继承的

授权的过程是所有更新的功能都是由新类的某部分来处理,已经存在的就授权给对象的默认属性

实现授权的关键点就是覆盖getattr方法

import time
class Open:
    def __init__(self,filepath,mode=‘r‘,encoding=‘utf-8‘):
        self.x = open(filepath,mode=mode,encoding=encoding)  # self.x是文件句柄 拿到文件中的内容
        self.filepath=filepath
        self.mode=mode
        self.encoding=encoding
    def write(self,line):  # 在类中新建write功能
        t=time.strftime(‘%Y-%m-%d %X‘)
        self.x.write("%s %s" %(t,line))  # self.x存放的是文件句柄
    def __getattr__(self, item):  # 通过getattr实现的叫做授权
        # print(‘---->‘,item,type(item))  # 测试查看类型
        return getattr(self.x,item)  # 从一个文件对象获得 字符串   现在获得的是self.x 相应方法的地址
f=Open(‘b.txt‘,‘w‘) #实例化一个对象 传入相应的参数
f.write(‘123\n‘)   #Open没有实际的writearttribute

f.write(‘123\n‘)
f.write(‘123\n‘)
f.write(‘123\n‘)
f.write(‘123\n‘)
f.write(‘123\n‘)
f.write(‘123\n‘)

这样就能在文件中添加了带有时间的内容

对文件进行读操作:

f=Open(‘b.txt‘,‘r‘)  # 重新实例化
res = f.read()# f.read实际是self.x 的方法
print(res)
f.seek(0)  #此时光标位于文件的最后
print(f.read())  #这样就可以重新读文件

div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin: 0 0 10px; padding: 8px 10px 0; font: 13.34px/1.4 helvetica, arial, freesans, clean, sans-serif; width: 452px; background-color: #fff }
div.oembedall-githubrepos .oembedall-body { background: -webkit-gradient(linear,left top,left bottom,from(#FAFAFA),to(#EFEFEF)); border-top: 1px solid #EEE; margin-left: -10px; margin-top: 8px; padding: 5px 10px; width: 100% }
div.oembedall-githubrepos h3 { font-size: 14px; margin: 0; padding-left: 18px; white-space: nowrap }
div.oembedall-githubrepos p.oembedall-description { color: #444; font-size: 12px; margin: 0 0 3px }
div.oembedall-githubrepos p.oembedall-updated-at { color: #888; font-size: 11px; margin: 0 }
div.oembedall-githubrepos ul.oembedall-repo-stats { border: none; float: right; font-size: 11px; font-weight: 700; padding-left: 15px; position: relative; z-index: 5; margin: 0 }
div.oembedall-githubrepos ul.oembedall-repo-stats li { border: none; color: #666; display: inline-block; list-style-type: none; margin: 0 !important }
div.oembedall-githubrepos ul.oembedall-repo-stats li a { background-color: transparent; border: none; color: #666 !important; background-position: 5px -2px; background-repeat: no-repeat; border-left: 1px solid #DDD; display: inline-block; height: 21px; line-height: 21px; padding: 0 5px 0 23px }
div.oembedall-githubrepos ul.oembedall-repo-stats li:first-child a { border-left: medium none; margin-right: -3px }
div.oembedall-githubrepos ul.oembedall-repo-stats li a:hover { background: 5px -27px no-repeat #4183C4; color: #FFF !important; text-decoration: none }
div.oembedall-githubrepos ul.oembedall-repo-stats li:first-child a:hover { }
ul.oembedall-repo-stats li:last-child a:hover { }
span.oembedall-closehide { background-color: #aaa; cursor: pointer; margin-right: 3px }
div.oembedall-container { margin-top: 5px; text-align: left }
.oembedall-ljuser { font-weight: 700 }
.oembedall-ljuser img { vertical-align: bottom; border: 0; padding-right: 1px }
.oembedall-stoqembed { border-bottom: 1px dotted #999; float: left; overflow: hidden; width: 730px; line-height: 1; background: #FFF; color: #000; font-family: Arial, Liberation Sans, DejaVu Sans, sans-serif; font-size: 80%; text-align: left; margin: 0; padding: 0 }
.oembedall-stoqembed a { color: #07C; text-decoration: none; margin: 0; padding: 0 }
.oembedall-stoqembed a:hover { text-decoration: underline }
.oembedall-stoqembed a:visited { color: #4A6B82 }
.oembedall-stoqembed h3 { font-family: Trebuchet MS, Liberation Sans, DejaVu Sans, sans-serif; font-size: 130%; font-weight: 700; margin: 0; padding: 0 }
.oembedall-stoqembed .oembedall-reputation-score { color: #444; font-size: 120%; font-weight: 700; margin-right: 2px }
.oembedall-stoqembed .oembedall-user-info { height: 35px; width: 185px }
.oembedall-stoqembed .oembedall-user-info .oembedall-user-gravatar32 { float: left; height: 32px; width: 32px }
.oembedall-stoqembed .oembedall-user-info .oembedall-user-details { float: left; margin-left: 5px; overflow: hidden; white-space: nowrap; width: 145px }
.oembedall-stoqembed .oembedall-question-hyperlink { font-weight: 700 }
.oembedall-stoqembed .oembedall-stats { background: #EEE; margin: 0 0 0 7px; padding: 4px 7px 6px; width: 58px }
.oembedall-stoqembed .oembedall-statscontainer { float: left; margin-right: 8px; width: 86px }
.oembedall-stoqembed .oembedall-votes { color: #555; padding: 0 0 7px; text-align: center }
.oembedall-stoqembed .oembedall-vote-count-post { font-size: 240%; color: #808185; display: block; font-weight: 700 }
.oembedall-stoqembed .oembedall-views { color: #999; padding-top: 4px; text-align: center }
.oembedall-stoqembed .oembedall-status { margin-top: -3px; padding: 4px 0; text-align: center; background: #75845C; color: #FFF }
.oembedall-stoqembed .oembedall-status strong { color: #FFF; display: block; font-size: 140% }
.oembedall-stoqembed .oembedall-summary { float: left; width: 635px }
.oembedall-stoqembed .oembedall-excerpt { line-height: 1.2; margin: 0; padding: 0 0 5px }
.oembedall-stoqembed .oembedall-tags { float: left; line-height: 18px }
.oembedall-stoqembed .oembedall-tags a:hover { text-decoration: none }
.oembedall-stoqembed .oembedall-post-tag { background-color: #E0EAF1; border-bottom: 1px solid #3E6D8E; border-right: 1px solid #7F9FB6; color: #3E6D8E; font-size: 90%; line-height: 2.4; margin: 2px 2px 2px 0; padding: 3px 4px; text-decoration: none; white-space: nowrap }
.oembedall-stoqembed .oembedall-post-tag:hover { background-color: #3E6D8E; border-bottom: 1px solid #37607D; border-right: 1px solid #37607D; color: #E0EAF1 }
.oembedall-stoqembed .oembedall-fr { float: right }
.oembedall-stoqembed .oembedall-statsarrow { background-image: url("http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3"); background-repeat: no-repeat; overflow: hidden; background-position: 0 -435px; float: right; height: 13px; margin-top: 12px; width: 7px }
.oembedall-facebook1 { border: 1px solid #1A3C6C; padding: 0; font: 13.34px/1.4 verdana; width: 500px }
.oembedall-facebook2 { background-color: #627add }
.oembedall-facebook2 a { color: #e8e8e8; text-decoration: none }
.oembedall-facebookBody { background-color: #fff; vertical-align: top; padding: 5px }
.oembedall-facebookBody .contents { display: inline-block; width: 100% }
.oembedall-facebookBody div img { float: left; margin-right: 5px }
div.oembedall-lanyard { background-attachment: scroll; background-color: transparent; background-image: none; border-width: 0; color: #112644; display: block; float: left; font-family: "Trebuchet MS", Trebuchet, sans-serif; font-size: 16px; height: 253px; line-height: 19px; margin: 0; max-width: none; min-height: 0; outline: #112644 0; padding: 0; position: relative; text-align: left; vertical-align: baseline; width: 804px }
div.oembedall-lanyard .tagline { font-size: 1.5em }
div.oembedall-lanyard .wrapper { overflow: hidden; clear: both }
div.oembedall-lanyard .split { float: left; display: inline }
div.oembedall-lanyard .prominent-place .flag:active,div.oembedall-lanyard .prominent-place .flag:focus,div.oembedall-lanyard .prominent-place .flag:hover,div.oembedall-lanyard .prominent-place .flag:link,div.oembedall-lanyard .prominent-place .flag:visited { float: left; display: block; width: 48px; height: 48px; position: relative; top: -5px; margin-right: 10px }
div.oembedall-lanyard .place-context { font-size: .889em }
div.oembedall-lanyard .prominent-place .sub-place { display: block }
div.oembedall-lanyard .prominent-place { font-size: 1.125em; line-height: 1.1em; font-weight: 400 }
div.oembedall-lanyard .main-date { color: #8CB4E0; font-weight: 700; line-height: 1.1 }
div.oembedall-lanyard .first { width: 48.57%; margin: 0 0 0 2.857% }
.mermaid .label { color: #333 }
.node circle,.node polygon,.node rect { }
.edgePath .path { }
.cluster rect { }
.cluster text { }
.actor { }
text.actor { }
.actor-line { }
.messageLine0 { }
.messageLine1 { }
#arrowhead { }
#crosshead path { }
.messageText { }
.labelBox { }
.labelText,.loopText { }
.loopLine { }
.note { }
.noteText { font-family: "trebuchet ms", verdana, arial; font-size: 14px }
.section { opacity: .2 }
.section0,.section2 { }
.section1,.section3 { opacity: .2 }
.sectionTitle0,.sectionTitle1,.sectionTitle2,.sectionTitle3 { }
.sectionTitle { font-size: 11px }
.grid .tick { opacity: .3 }
.grid path { }
.today { }
.task { }
.taskText { font-size: 11px }
.taskTextOutsideRight { font-size: 11px }
.taskTextOutsideLeft { font-size: 11px }
.taskText0,.taskText1,.taskText2,.taskText3 { }
.task0,.task1,.task2,.task3 { }
.taskTextOutside0,.taskTextOutside1,.taskTextOutside2,.taskTextOutside3 { }
.active0,.active1,.active2,.active3 { }
.activeText0,.activeText1,.activeText2,.activeText3 { }
.done0,.done1,.done2,.done3 { }
.doneText0,.doneText1,.doneText2,.doneText3 { }
.crit0,.crit1,.crit2,.crit3 { }
.activeCrit0,.activeCrit1,.activeCrit2,.activeCrit3 { }
.doneCrit0,.doneCrit1,.doneCrit2,.doneCrit3 { cursor: pointer }
.activeCritText0,.activeCritText1,.activeCritText2,.activeCritText3,.doneCritText0,.doneCritText1,.doneCritText2,.doneCritText3 { }
.titleText { font-size: 18px }
text { font-family: "trebuchet ms", verdana, arial; font-size: 14px }
html { height: 100% }
body { margin: 0 !important; padding: 5px 20px 26px !important; background-color: #fff; font-family: "Lucida Grande", "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", "Lucida Sans Unicode", Helvetica, Arial, sans-serif; font-size: .9em }
br,h1,h2,h3,h4,h5,h6 { clear: both }
hr.page { background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAECAYAAACtBE5DAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OENDRjNBN0E2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OENDRjNBN0I2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4Q0NGM0E3ODY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo4Q0NGM0E3OTY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PqqezsUAAAAfSURBVHjaYmRABcYwBiM2QSA4y4hNEKYDQxAEAAIMAHNGAzhkPOlYAAAAAElFTkSuQmCC") repeat-x; border: 0; height: 3px; padding: 0 }
hr.underscore { border-top-style: dashed !important }
body>:first-child { margin-top: 0 !important }
img.plugin { }
iframe { border: 0 }
figure { }
kbd { border: 1px solid #aaa; background-color: #f9f9f9; background-image: linear-gradient(top,#eee,#f9f9f9,#eee); padding: 1px 3px; font-family: inherit; font-size: .85em }
.oembeded .oembed_photo { display: inline-block }
img[data-echo] { margin: 25px 0; width: 100px; height: 100px; background: url("../img/ajax.gif") center center no-repeat #fff }
.spinner { display: inline-block; width: 10px; height: 10px; margin-bottom: -.1em; border: 2px solid rgba(0,0,0,.5); border-top-color: transparent }
.spinner::after { content: ""; display: block; width: 0; height: 0; position: absolute; top: -6px; left: 0; border: 4px solid transparent; border-bottom-color: rgba(0,0,0,.5) }
p.toc { margin: 0 !important }
p.toc ul { padding-left: 10px }
p.toc>ul { padding: 10px; margin: 0 10px; display: inline-block; border: 1px solid #ededed }
p.toc li,p.toc ul { list-style-type: none }
p.toc li { width: 100%; padding: 0; overflow: hidden }
p.toc li a::after { content: "." }
p.toc li a::before { content: "? " }
p.toc h5 { text-transform: uppercase }
p.toc .title { float: left; padding-right: 3px }
p.toc .number { margin: 0; float: right; padding-left: 3px; background: #fff; display: none }
input.task-list-item { margin-left: -1.62em }
.markdown { font-family: "Hiragino Sans GB", "Microsoft YaHei", STHeiti, SimSun, "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "Segoe UI", AppleSDGothicNeo-Medium, "Malgun Gothic", Verdana, Tahoma, sans-serif; padding: 20px }
.markdown a { text-decoration: none; vertical-align: baseline }
.markdown a:hover { text-decoration: underline }
.markdown h1 { font-size: 2.2em; font-weight: 700; margin: 1.5em 0 1em }
.markdown h2 { font-size: 1.8em; font-weight: 700; margin: 1.275em 0 .85em }
.markdown h3 { font-size: 1.6em; font-weight: 700; margin: 1.125em 0 .75em }
.markdown h4 { font-size: 1.4em; font-weight: 700; margin: .99em 0 .66em }
.markdown h5 { font-size: 1.2em; font-weight: 700; margin: .855em 0 .57em }
.markdown h6 { font-size: 1em; font-weight: 700; margin: .75em 0 .5em }
.markdown h1+p,.markdown h1:first-child,.markdown h2+p,.markdown h2:first-child,.markdown h3+p,.markdown h3:first-child,.markdown h4+p,.markdown h4:first-child,.markdown h5+p,.markdown h5:first-child,.markdown h6+p,.markdown h6:first-child { margin-top: 0 }
.markdown hr { border: 1px solid #ccc }
.markdown p { margin: 1em 0 }
.markdown ol { list-style-type: decimal }
.markdown li { display: list-item; line-height: 1.4em }
.markdown blockquote { margin: 1em 20px }
.markdown blockquote>:first-child { margin-top: 0 }
.markdown blockquote>:last-child { margin-bottom: 0 }
.markdown blockquote cite::before { content: "—?" }
.markdown .code { }
.markdown pre { border: 1px solid #ccc; overflow: auto; padding: .5em }
.markdown pre code { border: 0; display: block }
.markdown pre>code { font-family: Consolas, Inconsolata, Courier, monospace; font-weight: 700; white-space: pre; margin: 0 }
.markdown code { border: 1px solid #ccc; padding: 0 5px; margin: 0 2px }
.markdown img { max-width: 100% }
.markdown mark { color: #000; background-color: #fcf8e3 }
.markdown table { padding: 0; border-collapse: collapse; border-spacing: 0; margin-bottom: 16px }
.markdown table tr td,.markdown table tr th { border: 1px solid #ccc; margin: 0; padding: 6px 13px }
.markdown table tr th { font-weight: 700 }
.markdown table tr th>:first-child { margin-top: 0 }
.markdown table tr th>:last-child { margin-bottom: 0 }
.markdown table tr td>:first-child { margin-top: 0 }
.markdown table tr td>:last-child { margin-bottom: 0 }
.github { padding: 20px; font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", STHeiti, SimSun, "Segoe UI", AppleSDGothicNeo-Medium, "Malgun Gothic", Arial, freesans, sans-serif; font-size: 15px; background: #fff; line-height: 1.6 }
.github a { color: #3269a0 }
.github a:hover { color: #4183c4 }
.github h2 { border-bottom: 1px solid #e6e6e6; line-height: 1.6 }
.github h6 { color: #777 }
.github hr { border: 1px solid #e6e6e6 }
.github pre>code { font-size: .9em; font-family: Consolas, Inconsolata, Courier, monospace }
.github blockquote>code,.github h1>code,.github h2>code,.github h3>code,.github h4>code,.github h5>code,.github h6>code,.github li>code,.github p>code,.github td>code { background-color: rgba(0,0,0,.07); font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 85%; padding: .2em .5em; border: 0 }
.github blockquote { border-left: 4px solid #e6e6e6; padding: 0 15px; font-style: italic }
.github table { background-color: #fafafa }
.github table tr td,.github table tr th { border: 1px solid #e6e6e6 }
.github table tr:nth-child(2n) { background-color: #f2f2f2 }
.hljs-comment,.hljs-title { color: #9c9491 }
.css .hljs-class,.css .hljs-id,.css .hljs-pseudo,.hljs-attribute,.hljs-regexp,.hljs-tag,.hljs-variable,.html .hljs-doctype,.ruby .hljs-constant,.xml .hljs-doctype,.xml .hljs-pi,.xml .hljs-tag .hljs-title { color: #f22c40 }
.hljs-built_in,.hljs-constant,.hljs-literal,.hljs-number,.hljs-params,.hljs-pragma,.hljs-preprocessor { color: #df5320 }
.css .hljs-rules .hljs-attribute,.hljs-ruby .hljs-class .hljs-title { color: #d5911a }
.hljs-header,.hljs-inheritance,.hljs-string,.hljs-value,.ruby .hljs-symbol,.xml .hljs-cdata { color: #5ab738 }
.css .hljs-hexcolor { color: #00ad9c }
.coffeescript .hljs-title,.hljs-function,.javascript .hljs-title,.perl .hljs-sub,.python .hljs-decorator,.python .hljs-title,.ruby .hljs-function .hljs-title,.ruby .hljs-title .hljs-keyword { color: #407ee7 }
.hljs-keyword,.javascript .hljs-function { color: #6666ea }
.hljs { display: block; background: #2c2421; color: #a8a19f; padding: .5em }
.coffeescript .javascript,.javascript .xml,.tex .hljs-formula,.xml .css,.xml .hljs-cdata,.xml .javascript,.xml .vbscript { opacity: .5 }
.MathJax_Hover_Frame { border: 1px solid #A6D !important; display: inline-block; position: absolute }
.MathJax_Hover_Arrow { position: absolute; width: 15px; height: 11px; cursor: pointer }
#MathJax_About { position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDD; color: #000; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; white-space: nowrap; float: none; z-index: 201 }
.MathJax_Menu { position: absolute; background-color: #fff; color: #000; width: auto; padding: 2px; border: 1px solid #CCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; white-space: nowrap; float: none; z-index: 201 }
.MathJax_MenuItem { padding: 2px 2em; background: 0 0 }
.MathJax_MenuArrow { position: absolute; right: .5em; color: #666 }
.MathJax_MenuActive .MathJax_MenuArrow { color: #fff }
.MathJax_MenuArrow.RTL { left: .5em; right: auto }
.MathJax_MenuCheck { position: absolute; left: .7em }
.MathJax_MenuCheck.RTL { right: .7em; left: auto }
.MathJax_MenuRadioCheck { position: absolute; left: 1em }
.MathJax_MenuRadioCheck.RTL { right: 1em; left: auto }
.MathJax_MenuLabel { padding: 2px 2em 4px 1.33em; font-style: italic }
.MathJax_MenuRule { border-top: 1px solid #CCC; margin: 4px 1px 0 }
.MathJax_MenuDisabled { color: GrayText }
.MathJax_MenuActive { background-color: Highlight; color: HighlightText }
.MathJax_Menu_Close { position: absolute; width: 31px; height: 31px; top: -15px; left: -15px }
#MathJax_Zoom { position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid #000; margin: 0; font-weight: 400; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; white-space: nowrap; float: none }
#MathJax_ZoomOverlay { position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: #fff; opacity: 0 }
#MathJax_ZoomFrame { position: relative; display: inline-block; height: 0; width: 0 }
#MathJax_ZoomEventTrap { position: absolute; left: 0; top: 0; z-index: 302; display: inline-block; border: 0; padding: 0; margin: 0; background-color: #fff; opacity: 0 }
.MathJax_Preview { color: #888 }
#MathJax_Message { position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0; padding: 2px 8px; z-index: 102; color: #000; font-size: 80%; width: auto; white-space: nowrap }
#MathJax_MSIE_Frame { position: absolute; top: 0; left: 0; width: 0; z-index: 101; border: 0; margin: 0; padding: 0 }
.MathJax_Error { color: #C00; font-style: italic }
footer { position: fixed; font-size: .8em; text-align: right; bottom: 0; margin-left: -25px; height: 20px; width: 100% }

时间: 2024-11-09 06:23:14

Python 面向对象进阶的相关文章

Python面向对象进阶和socket网络编程-day08

写在前面 上课第八天,打卡: 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __init__(self,name): self.name=name p = Chinese('standby') # 实例化一个对象 print(p) # 打印这个对象 --- <__main__.Chinese object at 0x0000000000B3A978> - 示例2: >&g

python面向对象进阶版

面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个'函数'供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板创建的实例(即:对象),实例用于被包装在类中的函数: 4.面向对象三大特性:封装.继承和多态. 面向对象进阶篇详细介绍python类的成员.成员修饰符和类的特殊成员. 类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存

python面向对象进阶

isinstance(obj,cls)检查是否obj是否是类 cls 的对象. isinstance(obj,cls)检查是否obj是否是类 cls 的对象. 反射 python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 四个可以实现自省的函数(参数是对象,字符串格式的属性名) hasattr getattr setattr delattr __del__,就是析构方法,当对象在内存中被释放时,自动触发执行. 注:此方法一般无须定义,

python 面向对象 进阶篇

在上篇<python面向对象>中,简单介绍了python中面向对象的基本知识 在这篇博客中,详细介绍python类的成员,成员修饰符,类的特殊成员. 类的成员 类的成员分为三种:字段,方法和属性 所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的多少,在内存中只创建一份. 字段 字段包括普通字段和静态字段.静态字段,保存在类中.普通字段,保存在对象中. class FOO: country = “中国

python面向对象进阶(八)

上一篇<Python 面向对象初级(七)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实例用于调用被包装在类中的函数 面向对象三大特性:封装.继承和多态 本篇将详细介绍Python 类的成员.成员修饰符.类的特殊成员. 类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象

Python面向对象进阶及类成员

再次了解多继承 先来一段代码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ class A:    def bar(self):        print("BAR")        self.f1() class B(A):    def f1(self):        print("B") class C:    def f1(self):        print("C") class D(C,

Python面向对象进阶之高级编程

__slots__:定义类时,使用__slots__变量可以限制能添加的实例的属性 形如:__slots__ = ['name','age'] 这样实例化的对象只能绑定到name和age属性,其他属性则无法被绑定 class People:     __slots__ = ['name','age']     def __init__(self,name,age):         self.name = name         self.age = age p = People('laowa

Python之路-python(面向对象进阶)

一.面向对象高级语法部分 1.静态方法.类方法.属性方法 2.类的特殊方法 3.反射 二.异常处理 三.Socket开发基础 一.面向对象高级语法部分 静态方法(@staticmethod) 定义:只是名义上归类管理,实际上在在静态方法里面访问不了类或实例中的属性 1 class Dog(object): 2 def __init__(self,name): 3 self.name = name 4 5 @staticmethod 6 def eat(x,s): 7 print("%s is e

Python 面向对象进阶(一)

1. 生成器 通过列表生成式,可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个 包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数 的元素占用的空间都白白浪费了.所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的 过程中不断推算出后续的元素呢?这样,就不必创建完整的list,从而节省大量的空间. 在Python中,这种一边循环一边计算的机制,称为生成器(generator) # 创建生成器的方法一:只要