Python内置方法和属性应用:反射和单例(推荐)

作者:wedo实验君 时间:2022-08-04 03:23:48 

1. 前言

python除了丰富的第三方库外,本身也提供了一些内在的方法和底层的一些属性,大家比较常用的如dict、list、set、min、max、range、sorted等。笔者最近在做项目框架时涉及到一些不是很常用的方法和属性,在本文中和大家做下分享。

2. 内置方法和函数介绍  

enumerate

如果你需要遍历可迭代的对象,有需要获取它的序号,可以用enumerate, 每一个next返回的是一个tuple


list1 = [1, 2, 3, 4]
list2 = [4, 3, 2, 1]
for idx, value in enumerate(list1):
print(idx, value, list2[idx])
# 0 1 4
# 1 2 3
# 2 3 2
# 3 4 1

 zip zip从参数中的多个迭代器取元素组合成一个新的迭代器; 


# 给list加上序号
b = [4, 3, 2, 1]
for i in zip(range(len(b)), b):
print(i)
# (0, 4)
# (1, 3)
# (2, 2)
# (3, 1)
  •  globals():一个描述当前执行过程中全局符号表的字典,可以看出你执行的所有过程

  •  id(object):python对象的唯一标识

  •  staticmethod 类静态函数注解    


@staticmethod
def test():
print('this is static method')
Foo.test = test
Foo.test()

 类的属性 我们来看下一个类的申明,如下:   


class Foo():
"""this is test class"""
def __init__(self, name):
self.name = name
def run(self):
print('running')

# 列出类的所有成员和属性
dir(Foo)
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'run']
# 类的注释
Foo.__doc__
# 'this is test class'
# 类自定义属性
Foo.__dict__
mappingproxy({'__module__': '__main__',
'__doc__': 'this is test class',
'__init__': <function __main__.Foo.__init__(self, name)>,
'run': <function __main__.Foo.run(self)>,
'__dict__': <attribute '__dict__' of 'Foo' objects>,
'__weakref__': <attribute '__weakref__' of 'Foo' objects>})
# 类的父类
Foo.__base__
# 类的名字
Foo.__name__

类的实例化和初始化


# python类先通过__new__实例化,再调用__init__进行初始化类成员
foo = Foo('milk')

类的属性添加和访问


# 类的访问
foo.name
foo.run()
# 可以通过setattr 动态的添加属性
def method():
print("cow")
setattr(foo, "type", "cow")
setattr(foo, "getcow", method)
# cow
foo.type
foo.getcow()
# 动态删除属性 delattr
delattr(foo, "type")
# getattr 获取成员属性
if hasattr(foo, "run"): # 判断是否有属性
func = getattr(foo, "run")
func()

3. 单例模式应用

单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。单例模式要求在类的使用过程中只实例化一次,所有对象都共享一个实例。创建的方法是在实例的时候判断下是否已经实例过了,有则返回实例化过的全局实例。python是如何实现的呢?关键是找到实例化的地方,对就是前面说的__new__


class Singleton(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = object.__new__(cls)
return cls._instance
def __init__(self, name):
self.name = name
a = Singleton('name1')
b = Singleton('name2')
print(id(a), id(b))
print(a.name, b.name)
# 1689352213112 1689352213112
# name2 name2

4. 反射应用

反射在许多框架中都有使用到,简单就是通过类的名称(字符串)来实例化类。一个典型的场景就是通过配置的方式来动态控制类的执行,比如定时任务的执行,通过维护每个定时任务类的执行时间,在执行时间到的时候,通过反射方式实例化类,执行任务,在java中也非常的常见。

python的实现可以通过上面说的getattr获取模块中的类, 通过methodcaller来调用方法。我们来看一个简单的例子


import importlib
from operator import methodcaller
class Foo():
"""this is test class"""
def __init__(self, name):
self.name = name
def run(self, info):
print('running %s' % info)
# 类所在的模块,默认情况__main__, 可以通过Foo.__dict__ 中'__module__'获取
api_module = importlib.import_module('__main__')
# getattr获取模块中的类, 这里Foo是字符串哦
clazz = getattr(api_module, 'Foo')
# 实例化
params = ["milk"]
instance = clazz(*params)
# 方法调用, 方法也是字符串methodcaller(方法名, 方法参数)
task_result = methodcaller("run", "reflection")(instance)
# running reflection

5. 总结

本文通过分享了python内置方法和属性, 并在单例模式和反射中进行应用。希望对你有帮助,欢迎交流@mintel 要点总结如下:

  • dir下类

  •  查看类自定义属性__dict__

  •  __new__实例化类,__init__初始化类

  •  getattr 获取属性

  •  setattr 设置属性

  •  记住importlib和methodcaller

来源:http://developer.51cto.com/art/202006/619255.htm

标签:Python,内置方法,属性,反射,单例
0
投稿

猜你喜欢

  • asp 在线备份与恢复sqlserver数据库的代码

    2011-03-06 11:14:00
  • Python设计模式之观察者模式简单示例

    2023-07-12 04:36:06
  • 编写和优化SQL Server的存储过程

    2009-04-13 10:13:00
  • python 模拟银行转账功能过程详解

    2021-07-16 02:49:15
  • 让JavaScript拯救HTML5的离线存储[译]

    2009-05-15 12:26:00
  • asp如何将RGB颜色转化成到16进制的?

    2009-11-26 20:41:00
  • python爬虫之爬取百度音乐的实现方法

    2022-09-02 06:22:10
  • 分析并输出Python代码依赖的库的实现代码

    2022-04-05 17:07:23
  • 用Pytorch训练CNN(数据集MNIST,使用GPU的方法)

    2021-10-12 23:01:01
  • Flask框架模板渲染操作简单示例

    2023-06-07 00:02:45
  • pygame实现弹球游戏

    2023-05-26 20:38:44
  • Python函数高级(命名空间、作用域、装饰器)

    2022-03-15 23:31:44
  • django从请求到响应的过程深入讲解

    2021-08-09 19:41:06
  • Python无法用requests获取网页源码的解决方法

    2023-04-24 07:38:04
  • python io.BytesIO简介及示例代码

    2021-04-25 04:52:31
  • javascript创建数组的最简代码

    2013-09-01 21:43:04
  • rs.open sql,conn,1,1全接触

    2007-11-01 22:46:00
  • python获取txt文件词向量过程详解

    2021-07-27 12:54:35
  • 使用Python从零开始撸一个区块链

    2021-06-02 07:28:22
  • python GUI库图形界面开发之PyQt5窗口类QMainWindow详细使用方法

    2021-03-16 08:53:47
  • asp之家 网络编程 m.aspxhome.com