1.item

# __getitem__ __setitem__ __delitem__    obj['属性']操作触发

class Foo:
def __getitem__(self, item):
return self.__dict__[item] def __setitem__(self, key, value):
self.__dict__[key] = value def __delitem__(self, key):
self.__dict__.pop(key) f = Foo()
f.name = 'zq'
f.age =
print(f.__dict__) del f.name
print(f.__dict__) print(f.age)

2.str repr

class Foo:
def __init__(self,name,age):
self.name = name
self.age = age def __str__(self):
return "定制__str__ %s %s" %(self.name,self.age) def __repr__(self):
return "定制的__repr__ 替换__str__" f = Foo("zhangsan","") # 改变对象的字符串显示
# __str__
# __repr__ 在解释器中触发 当和str同时存在执行str,当str不存在,执行repr,是str的替代者
# str(f) > f.__str__()> "定制__str__"
print(f) # 定制__str__ zhangsan 12

3.format定制

# 自定制 format
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day def __format__(self, format_spec=""):
return "{0.year}{format_spec}{0.month}{format_spec}{0.day}".format(self, format_spec=format_spec) d1 = Date('', '', '')
print(format(d1)) #
print(format(d1, ':')) # 2018:12:12

4.slot 类变量

# __slots__ 是一个类变量 可以是list tunple iter...
# 减少内存使用 取代__dict__的属性 不再有__dict__
# 1.限制创建属性
class Foo:
__slots__ = ['name', 'age'] # 类似属性字典 定义字典中key {'name':None,'age':None} f1 = Foo()
f1.name = 'zhangsan'
f1.age = 100
# print(f1.__dict__) # AttributeError: 'Foo' object has no attribute '__dict__'
print(f1.__slots__) # ['name', 'age']
print(f1.name, f1.age)
# f1.gender = 'man' # AttributeError: 'Foo' object has no attribute 'gender'

5.doc

# __doc__ 文档注释 改属性不可被继承
class Foo:
'''
我是Foo文档信息
'''
pass class A(Foo):
'''
我是A文档信息
'''
pass print(Foo.__doc__) # 我是Foo文档信息
print(A.__doc__) # 我是A文档信息

6.module class

# __module__ 查看由那个module产生
# __class__ 查看由那个类产生
import c
c = c.c()
print(c.name)
print(c.__module__)
print(c.__class__)

7.析构方法 __del__ 当对象在内存中被释放时,自动触发执行

# __del__ 析构函数 删除实例才触发该函数
class Foo:
def __init__(self, name):
self.name = name def __del__(self):
print("__del__ 实例") f1 = Foo('zhangsan')
del f1.name # 先删除实例的属性 再执行下面语句 然后删除实例 执行析构函数
print("--------->")

8.call方法

# call 对象后面加(),触发执行
# call 类()执行 会触发__call__ 函数 Foo()
class Foo:
def __call__(self, *args, **kwargs):
print("触发实例 obj()") f1 = Foo()
f1()
Foo()

9.迭代器协议 next iter

# 迭代器协议:对象必须有一个next方法,执行一次返回迭代下一项,直到Stopiteration,
# __iter___ 把一个对象变成可迭代对象
class Foo:
def __init__(self, age):
self.age = age def __iter__(self):
return self def __next__(self):
if self.age == 197:
raise StopIteration("end")
self.age += 1
return self.age f1 = Foo(195)
print(f1.__next__())
print(f1.__next__())
# print(f1.__next__()) for i in f1: # for循环调用f1中的iter方法变成可迭代对象 再执行next方法 iter(f1)==f1.__iter__()
print(i) # 斐波那契数列
class Fib:
def __init__(self, num):
self._x = 1
self._y = 1
self.num = num def __iter__(self):
return self def __next__(self):
self._x, self._y = self._y, self._x + self._y
if self._x > self.num:
raise StopIteration("结束")
return self._x def show_fib(self):
l = []
for i in self:
l.append(i)
return l #求1000内的数
f1 = Fib(100000)
print(f1.show_fib())

10.描述符

描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一个,这也被称为描述符协议
__get__():调用一个属性时,触发
__set__():为一个属性赋值时,触发
__delete__():采用del删除属性时,触发

#描述符Str
class Str:
def __get__(self, instance, owner):
print('Str调用')
def __set__(self, instance, value):
print('Str设置...')
def __delete__(self, instance):
print('Str删除...') #描述符Int
class Int:
def __get__(self, instance, owner):
print('Int调用')
def __set__(self, instance, value):
print('Int设置...')
def __delete__(self, instance):
print('Int删除...') class People:
name=Str()
age=Int()
def __init__(self,name,age): #name被Str类代理,age被Int类代理,
self.name=name
self.age=age #何地?:定义成另外一个类的类属性 #何时?:且看下列演示 p1=People('alex',18) #描述符Str的使用
p1.name
p1.name='egon'
del p1.name #描述符Int的使用
p1.age
p1.age=18
del p1.age #我们来瞅瞅到底发生了什么
print(p1.__dict__)
print(People.__dict__) #补充
print(type(p1) == People) #type(obj)其实是查看obj是由哪个类实例化来的
print(type(p1).__dict__ == People.__dict__) 描述符应用之何时?何地?

注意事项:
一 描述符本身应该定义成新式类,被代理的类也应该是新式类
二 必须把描述符定义成这个类的类属性,不能为定义到构造函数中
三 要严格遵循该优先级,优先级由高到底分别是
1.类属性
2.数据描述符
3.实例属性
4.非数据描述符
5.找不到的属性触发__getattr__()

11.开发规范

# 软件开发规范 范例
'''
bin 脚本文件
start.py 启动文件 conf
settings.py 配置文件 db 用户数据
admin
classes
teacher
course
cource_to_school
student lib 公共类库
commons.py log 日志信息 src或core 核心文件 #导入模块
# BASER_DIR
import sys, os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

参考:http://www.cnblogs.com/linhaifeng/articles/6204014.html

python item repr doc format slots doc module class 析构 call 描述符的更多相关文章

  1. 如何正确地使用Python的属性和描述符

    关于@property装饰器 在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问. 那么为什么要这样做呢?因为@property让我们将自定义的代码同变量的访问/设定联 ...

  2. Python中属性和描述符的简单使用

    Python的描述符和属性是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题苦恼的 ...

  3. Python 中的属性访问与描述符

    在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个name属性,那便可以使用foo.name对此属性进行访问.一般而言,点(. ...

  4. Python中的属性访问与描述符

    Python中的属性访问与描述符 请给作者点赞--> 原文链接 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个nam ...

  5. python高级编程之描述符与属性02

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元描述符 #特点是:使用宿主类的一个或者多个方法来执行一个任务,可 ...

  6. Python描述符 (descriptor) 详解

    1.什么是描述符? python描述符是一个“绑定行为”的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有 __get__(), __set__(), 和__delete__().如 ...

  7. Python强大的格式化format

    原文地址:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型 ...

  8. (转)Python 字符串格式化 str.format 简介

    原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...

  9. python代码迷之错误(ModuleNotFoundError: No module named 'caffe.proto')

    1.pandas.read_csv book[n]下的print(n) 总图片数是少一张的,print(n)发现也是少了一个序号 仔细查找后发现缺少99号,即最后一张图片没有被读取.print(m)时 ...

随机推荐

  1. 【Alpha】任务分解与分配

    Alpha阶段总体任务规划 Alpha阶段我们的任务主要是恢复原先项目的代码运行,并增加一部分物理实验(二)的内容以及完善之前项目未完成的功能,例如后台管理及用户管理界面.在恢复项目部分的主要工作是将 ...

  2. windows server 2012 valid key

    好吧,网页三剑客. 1, load disc iso 2,check ip settings, 3,net-inst-server-start 4,power Node, F2 4.1 F7 usbc ...

  3. weiFenLuo.winFormsUI.Docking.dll学习

    引用方法: 1.建立一个WinForm工程,默认生成了一个WinForm窗体. 2.引用—>添加引用—>浏览—>weiFenLuo.winFormsUI.Docking.dll. 3 ...

  4. compact framework windows mobile wm c#代码 创建快捷方式

    已经2018年了,windows mobile已经宣布不维护狠多年了,不要问我为什么还在开发windows mobile的程序,我也不想.公司有一批手持扫描枪设备依然是windows mobile的程 ...

  5. 【Ubuntu】安装配置apahce

    安装apachesudo apt-get install apache2 安装目录 /etc/apache2配置文件 /etc/apache2/apache2.conf默认网站根目录 /var/www ...

  6. python_SMTP and POP3

    #!/usr/bin/python #coding=utf-8 #发送邮件 import smtplib from smtplib import SMTP as smtp import getpass ...

  7. 如何在CentOS7上安装桌面环境?

    1.安装 GNOME-Desktop 安装GNOME桌面环境 # yum -y groups install "GNOME Desktop" 完成安装后,使用如下命令启动桌面 # ...

  8. echo(),print(),print_r()之间的区别?

    echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)  print只能打印出简单类型变量的值(如int,string)  print_r可以打印出复 ...

  9. 四大CPU体系结构:ARM、X86/Atom、MIPS、PowerPC

    补充介绍一下RISC:RISC(reduced instruction set computer,精简指令集计算机)是一种执行较少类型计算机指令的微处理器,起源于80年代的MIPS主机(即RISC机) ...

  10. C# SocketUdpServer

    public interface ISocketUdpServer { void Start(); void Stop(); int SendData(byte[] data, IPEndPoint ...