Python3 面向对象进阶2
Classmethod
- classmethod是一个装饰器, 用来装饰类内部的方法, 使得该方法绑定给类来使用
- 对象绑定方法的特殊之处: 由对象调用, 将对象作为第一参数传给该方法
- 类的绑定方法的特殊之处: 由类来调用, 将类作为第一参数传给该方法
Staticmethod
- staticmethod是一个装饰器, 用来装饰类内部的方法, 使得该方法既不绑定给对象, 也不绑定给类
# settings.py
USER = 'bigb'
PASSWORD = '123'
import uuid
import settings
import hashlib
class User:
def __init__(self, username, password):
self.username = username
self.password = password
def admin(self):
if not self.username == 'bigb' and self.password == '123':
print('请用管理员账户登录...')
return
print('显示管理员功能...')
@classmethod
def __from_settings(cls):
obj = cls(settings.USER, settings.PASSWORD)
return obj
@staticmethod
def creat_id():
uuid_obj = uuid.uuid4()
md5 = hashlib.md5()
md5.update(str(uuid_obj).encode('utf-8'))
return md5.hexdigest()
obj1 = User._User__from_settings()
obj1.admin() # 显示管理员功能...
obj2 = User('blake', '123')
obj2.admin() # 请用管理员账户登录...
print(obj2.creat_id()) # e249b25cdd1963a4949cc99d3ad46ff2
Isinstance
isinstance(args1, args2)
Python内置函数, 用于判断args1
(对象)是否是args2
(类)的一个实例
class Foo:
pass
foo_obj = Foo()
print(isinstance(foo_obj, Foo)) # True
Issubclass
issubclass(args1, args2)
Python内置函数, 用于判断args1
是否是args2
的子类
class ParentClass:
pass
class ChildClass(ParentClass):
pass
class OtherClass:
pass
print(issubclass(ChildClass, ParentClass)) # True
print(issubclass(OtherClass, ParentClass)) # False
反射
概念
- 反射指的是通过字符串对对象或类的属性进行操作
hasattr
hasattr(o, attrname)
判断o
(类或对象) 中是否有名字叫attrname
(字符串) 这样的一个属性
class Chinese:
country = 'China'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
people = Chinese('bigb', '18', 'male')
print('name' in people.__dict__) # True
print(hasattr(people, 'name')) # True
getattr
getattr(o, attrname, default)
: 用来获取o
(对象或类) 中名叫attrname
(字符串) 的属性对象, 如果没有则返回默认值default
class Chinese:
country = 'China'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def say_slogan(self):
print('One China')
people = Chinese('bigb', '18', 'male')
people.say_slogan() # One China
getattr(people, 'say_slogan')() # One China
print(getattr(people, 'country')) # China
print(getattr(people, 'country1', 'China')) # China
class Movie:
def input_cmd(self):
while True:
cmd = input('请输入方法名: ')
if hasattr(self, cmd):
method = getattr(self, cmd)
method()
def upload(self):
print('电影开始上传...')
def download(self):
print('电影开始下载...')
movie_obj = Movie()
movie_obj.input_cmd()
setattr
setattr(o, attrname, value)
用来添加或修改o
(对象或类) 的属性:attrname = value
class Chinese:
country = 'China'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def say_slogan(self):
print('One China')
people = Chinese('bigb', '18', 'male')
# 修改
setattr(people, 'gender', 'female')
# 添加
setattr(people, 'province', 'Anhui')
print(people.gender) # female
print(people.province) # Anhui
delattr
delattr(o, attrname)
用来删除o
(对象或类) 中叫做attrname
(字符串) 的属性
class Chinese:
country = 'China'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def say_slogan(self):
print('One China')
people = Chinese('bigb', '18', 'male')
# 添加
setattr(people, 'province', 'Anhui')
# 删除
delattr(people, 'province')
print(people.province) # AttributeError: 'Chinese' object has no attribute 'province'
魔法方法
概念
- 在类内部定义, 以
__
开头__
结尾的方法的方法都称之为魔法方法, 又称为类的内置方法 - 魔法方法会在某些条件成立是触发
- 魔法方法都是object类内定义的方法
__new__
- 在调用类时自动触发 (return
self
给__init__
)
class Foo(object):
def __new__(cls, *args, **kwargs):
print('__new__')
return object.__new__(cls) # 产生了一个空对象
def __init__(self):
print('__init__')
foo_obj = Foo()
'''
__new__
__init__
'''
__init__
- 在创建完对象后自动触发
__init__
是实例化对象后触发的第一个方法(接收__new__
return 的self
)
__str__
- 在打印对象时自动触发
- 必须要有一个返回值, 该返回值必须是字符串类型
class Foo(object):
def __str__(self):
print('__str__在打印对象时自动触发')
return '__str__的返回值'
foo_obj = Foo()
print(foo_obj)
'''
__str__在打印对象时自动触发
__str__的返回值
'''
__call__
- 在对象被调用时自动触发
class Foo(object):
def __call__(self, *args, **kwargs):
print('__call__在对象被调用时自动触发')
foo_obj() # __call__在对象被调用时自动触发
__getattr__
- 在
对象.属性
时, 没有该属性时候自动触发
class Foo(object):
# 默认返回None
def __getattr__(self, item):
print('__getattr__会在对象.属性,且属性不存在时自动触发')
return item
foo_obj = Foo()
print(foo_obj.x)
'''
__getattr__会在对象.属性,且属性不存在时自动触发
x
'''
__setattr__
- 在
对象.属性=属性值
时自动触发
class Foo(object):
def __setattr__(self, key, value):
print('__setattr__在 对象.属性=属性值 时自动触发')
print(key, value)
foo_obj = Foo()
# 类中有__setattr__ 方法时, 如下操作只触发了__setattr__方法, 并不会给对象添加或修改属性
foo_obj.x = 1
'''
__setattr__在 对象.属性=属性值 时自动触发
x 1
'''
print(foo_obj.x) # AttributeError: 'Foo' object has no attribute 'x'
__del__
- 在对象被销毁后自动触发
class Foo(object):
def __del__(self):
print('__del__在对象被销毁后自动触发')
foo_obj = Foo()
print('对象被销毁前')
del foo_obj
print('程序最后的代码.')
'''
对象被销毁前
__del__在对象被销毁后自动触发
程序最后的代码
'''
class Myfile(object):
def __init__(self, file_name, mode='r', encoding='utf-8'):
self.file_name = file_name
self.mode = mode
self.encoding = encoding
def file_open(self):
self.f = open(self.file_name, self.mode, encoding=self.encoding)
def file_read(self):
data = self.f.read()
print(f'''
当前文件: {self.file_name}
当前文件数据: {data}
''')
def __del__(self):
self.f.close()
print('关闭文件成功!')
f = Myfile('test_file.txt')
f.file_open()
f.file_read()
'''
当前文件: test_file.txt
当前文件数据: This is the content of test_file.txt
关闭文件成功!
'''
单例模式
概念
- 单例模式指的是某个类在整个系统中只存在一个实例的一种设计模式
目的
- 减少内存占用
- 加快运行性能 (只初始化一次)
实现方式
class File(object):
__instance = None
# # 方式1, 通过类的绑定方法实现
# @classmethod
# def singleton(cls, file_name):
# # 当类没有实例时
# if not cls.__instance:
# # 创建实例
# cls.__instance = cls(file_name)
# return cls.__instance
# 方式2, 通过__new__方法实现
def __new__(cls, *args, **kwargs):
# 当类没有实例时
if not cls.__instance:
# 创建实例
cls.__instance = object.__new__(cls)
return cls.__instance
def __init__(self, file_name, mode='r', encoding='utf-8'):
self.file_name = file_name
self.mode = mode
self.encoding = encoding
def open(self):
self.f = open(self.file_name, self.mode, encoding=self.encoding)
def file_read(self):
data = self.f.read()
print(f'''
当前文件: {self.file_name}
当前文件数据: {data}
''')
def close(self):
self.f.close()
# obj1 = File.singleton('test_file.txt')
# obj2 = File.singleton('test_file.txt')
# obj3 = File.singleton('test_file.txt')
obj1 = File('test_file.txt')
obj2 = File('test_file.txt')
obj3 = File('test_file.txt')
print(obj1)
print(obj2)
print(obj3)
'''
<__main__.File object at 0x0000000009F87A58>
<__main__.File object at 0x0000000009F87A58>
<__main__.File object at 0x0000000009F87A58>
'''
Python3 面向对象进阶2的更多相关文章
- Python3 面向对象进阶1
目录 组合 概念 目的 实现方式 封装 概念 目的 实现方式 访问限制 概念 目的 实现方式 property 概念 目的 实现方式 多态 概念 目的 抽象类 概念 目的 实现方法 鸭子类型 组合 概 ...
- Python全栈开发【面向对象进阶】
Python全栈开发[面向对象进阶] 本节内容: isinstance(obj,cls)和issubclass(sub,super) 反射 __setattr__,__delattr__,__geta ...
- python基础——面向对象进阶下
python基础--面向对象进阶下 1 __setitem__,__getitem,__delitem__ 把对象操作属性模拟成字典的格式 想对比__getattr__(), __setattr__( ...
- Python面向对象进阶和socket网络编程-day08
写在前面 上课第八天,打卡: 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __i ...
- Python中级 —— 01面向对象进阶
面向对象进阶 总结.补充(http://blog.csdn.net/fgf00/article/details/52479307) 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 ...
- python_面向对象进阶(7)
第1章 面向对象特性—继承(补充) 1.1 接口类.抽象类介绍 1.2 接口类 1.3 接口类应用过程 1.3.1 第一版:完成多种支付方式接口 1.3.2 第二版: 归一化设计,统一支付方式 1.3 ...
- Python面向对象进阶和socket网络编程
写在前面 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __init__(self ...
- day021|python之面向对象进阶1
面向对象进阶 目录 面向对象进阶 1 继承 1.1 继承入门 1.1.1 继承基础 1.1.2 类的基本使用 1.2 多继承 1.2.1 多继承的基本使用 1.2.2 多继承以后的重复性 1.3 类的 ...
- day26、面向对象进阶:多态、封装、反射
一.多态 什么是多态: 类的继承有两层意义:1.改变 2.扩展 多态就是类的这两层意义的一个具体的实现机. 即:调用不同类实例化的对象,下的相同的方法,实现的过程不一样 python中的标准类型就是多 ...
随机推荐
- secureCRT连接虚拟机
1.secureCRT英文版下载 链接:https://pan.baidu.com/s/1LFWD-k2r4ZB7DHQA66QogQ 密码:khmo 破解方式参考 2.虚拟机静态IP设置 参考 3. ...
- nyoj 170-网络的可靠性 (度为1)
170-网络的可靠性 内存限制:64MB 时间限制:3000ms 特判: No 通过数:15 提交数:21 难度:3 题目描述: A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助 ...
- nyoj 733-万圣节派对 (printf("%06d", i))
733-万圣节派对 内存限制:64MB 时间限制:1000ms 特判: No 通过数:5 提交数:7 难度:1 题目描述: 万圣节有一个Party,XadillaX显然也要去凑热闹了.因为去凑热闹的人 ...
- nyoj 455-黑色帽子
455-黑色帽子 内存限制:64MB 时间限制:1000ms 特判: No 通过数:4 提交数:7 难度:1 题目描述: 最近发现了一个搞笑的游戏,不过目前还没玩过.一个舞会上,每个人 ...
- 领扣(LeetCode)设计哈希映射 个人题解
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
- 使用不同的C++支持库的模块混合开发时,引发异常展开不正常,抛异常竟引出一个SIGSEGV
如果你使用gcc对一部分模块进行了GNUMake的编译,这些编译出动态库使用在Gradle编译框架下的项目.那么就有可能出现题目中的情况,使用不同的C++支持库的模块混合开发时,引发异常展开不正常. ...
- elementui 模态框 拖动
第一步引入import elDragDialog from "@/directive/el-dragDialog";第二步 在export default中声明directives ...
- [Odoo12基础教程]之第一篇-创建Todo应用
声明: 本教程基于 Ruter 老师的 [Odoo基础教程系列] ,Ruter老师教程的链接地址为:Odoo基础教程系列 . 至于为什么已经有了Ruter老师的教程,还要自己再搬移一份呢?是基于一 ...
- pyenv virtualenv和virtualwrapper
pyenv pyenv最大的优势是:可以在”全局”管理不同版本的Python, 可以随时配置当前的使用的Python版本,并对其他使用Python解释器的程序生效.当系统安装多个版本的Python,使 ...
- https安全在哪里,原理是什么?
转载请标明出处:http://blog.csdn.net/shensky711/article/details/52214842 本文出自: [HansChen的博客] Https通信基本过程 在通信 ...