与任何类共享相同的属性。

class-propert的Python项目详细描述


安装

pip install class_property

公用事业

  • class_value - Hold a single value for a class and all instances.
  • class_property - Like a property, but works on MyClass.class_prop = value
  • metaclass - Create a metaclass that looks for class_value and class_property to register them.
  • decorate - Decorate a class to register any class_value and class_property.

类必须使用元类或装饰函数。子类不需要使用decorate或元类。 元类是类对象如何与描述符一起工作的。在

类值用法。在

示例:

^{pr2}$

类属性用法。在

示例:

fromclass_propertyimportclass_value,class_property,decorate,metaclassglobalGLOBGLOB='Hello'defget_glob():"""Return the global GLOB value"""globalGLOBreturnGLOBdefset_glob(value):globalGLOBGLOB=value# doesn't matter if class_value.decorate, class_property.decorate, or decorate (same with metaclass)classMyClass(object,metaclass=metaclass()):_VALUE=None@class_propertydefvalue(self):returnMyClass._VALUE@value.setterdefvalue(self,value):MyClass._VALUE=value# Also works with no arguments@class_propertydefvalue2():returnMyClass._VALUE@value2.setterdefvalue2(value):MyClass._VALUE=valueglob=class_property(get_glob,set_glob)mc=MyClass()assertmc.valueisNoneassertMyClass.valueisNoneMyClass.value=3assertmc.value==3assertMyClass.value==3mc.value=2assertmc.value==2assertMyClass.value==2assertmc.value2==2assertMyClass.value2==2mc.value2=5assertmc.value==5assertMyClass.value==5assertmc.value2==5assertMyClass.value2==5assertMyClass.glob=='Hello'assertmc.glob=='Hello'MyClass.glob='Jack'assertMyClass.glob=='Jack'assertmc.glob=='Jack'mc.glob='Jill'assertMyClass.glob=='Jill'assertmc.glob=='Jill'classSubClass(MyClass):passsub=SubClass()sub.glob='John'assertSubClass.glob=='John'assertsub.glob=='John'assertMyClass.glob=='John'assertmc.glob=='John'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在导入到Google工作表时使用ApachePOI显示系列标签   java在Swing表上修改数据生成SQL   java TCP数据包在网络级别合并   java自动连线bean在线程位置为空   javasocket。禁用无线连接时getOutputStream()阻塞   JSON上的javascript字符串数组。stringify输出unicode字符   java在Oracle数据库中存储不同类型数据的最佳体验   Spring MVC中模板引擎后的java进程输出   不知从哪里来的java空字符串。   如何加载java。使用Adobe Flex的客户端的属性文件?   java如何替换多层括号之间的所有内容?   {JSONObject必须以java'开头   java使用commandButton或commandLink返回并管理HTML控件值   java Android大文本视图动态   java JMock需要自定义类   java Android应用程序在emulator中工作,但在设备中失败   java连接到derby数据库时使用什么文件路径格式?   java在一个函数调用中返回两个结果?