雄安邯郸网站制作多少钱,网站开发职责与要求,账号交易网站数据库应该怎么做,安阳网站制作优化1. 前言python除了丰富的第三方库外#xff0c;本身也提供了一些内在的方法和底层的一些属性#xff0c;大家比较常用的如dict、list、set、min、max、range、sorted等。笔者最近在做项目框架时涉及到一些不是很常用的方法和属性#xff0c;在本文中和大家做下分享。2. 内置…1. 前言python除了丰富的第三方库外本身也提供了一些内在的方法和底层的一些属性大家比较常用的如dict、list、set、min、max、range、sorted等。笔者最近在做项目框架时涉及到一些不是很常用的方法和属性在本文中和大家做下分享。2. 内置方法和函数介绍enumerate如果你需要遍历可迭代的对象有需要获取它的序号可以用enumerate, 每一个next返回的是一个tuplelist1 [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 1zip 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 testFoo.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.namefoo.run()# 可以通过setattr 动态的添加属性def method(): print(cow)setattr(foo, type, cow)setattr(foo, getcow, method)# cowfoo.typefoo.getcow()# 动态删除属性 delattrdelattr(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._instancedef __init__(self, name):self.name namea 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 methodcallerclass Foo():this is test classdef __init__(self, name):self.name namedef 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小编在此分享一些python干货需要资料的私信小编“资料”即可免费领取哈