python怎么创建和使用一个简单的元类
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章给大家分享的是有关python怎么创建和使用一个简单的元类的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。示例#!/usr/bin/env python# [SNIP
千家信息网最后更新 2026年11月02日python怎么创建和使用一个简单的元类
这篇文章给大家分享的是有关python怎么创建和使用一个简单的元类的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
示例
#!/usr/bin/env python# [SNIPPET_NAME: Simple metaclass]# [SNIPPET_CATEGORIES: Python Core] # [SNIPPET_DESCRIPTION: Shows how to create a and use a simple metaclass]# [SNIPPET_AUTHOR: Florian Diesch]# [SNIPPET_LICENSE: MIT]# Copyright 2010 Florian Diesch # All rights reserved.## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.#class PropertyMetaclass(type): """ This metaclass expects its instances to have a class attribute __properties__ that is a dict mapping property names to their default values. The metaclass creates the corresponding properties """ def __init__(cls, name, bases, dict): type.__init__(cls, name, bases, dict) props = getattr(cls, '__properties__', {}) for name, default in props.iteritems(): type(cls).create_property(cls, name, default) def attr_name(cls, prop): """ returns the attribute name for property """ return '_%s'%prop def create_property(cls, name, default): """ creates a property named with default value """ getter=cls.create_getter(name) setter=cls.create_setter(name) prop=property(getter, setter, None, None) # that's the attribute holding the actual value setattr(cls, cls.attr_name(name), default) # that's the property setattr(cls, name, prop) def create_getter(cls, prop): """ creates a getter method for property """ attr=cls.attr_name(prop) def getter(self): print " class %s: get %s"%(cls.__name__, prop) return getattr(self, attr) return getter def create_setter(cls, prop): """ creates a setter method for property """ attr=cls.attr_name(prop) def setter(self, value): print " class %s: set %s to '%s'"%(cls.__name__, prop, value) setattr(self, attr, value) return setterclass Book(object): __metaclass__= PropertyMetaclass # use the metaclass __properties__ = { # some properties 'author': '[unknown title]', 'title': '[unknown author]' }if __name__ == '__main__': book = Book() print '%s by %s'%(book.title, book.author) book.author = 'Euclid' book.title = 'Elements' print '%s by %s'%(book.title, book.author) # prints: # # class Book: get title # class Book: get author # --unknown author-- by --unknown title-- # class Book: set author to 'Euclid' # class Book: set title to 'Elements' # class Book: get title # class Book: get author # Elements by Euclid
感谢各位的阅读!关于"python怎么创建和使用一个简单的元类"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
内容
更多
篇文章
不错
实用
文章
看吧
知识
示例
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网络科技
高科技互联网
互联网和科技创新
极限挑战互联网科技
互联网金融科技创新
智慧科技互联网
佰商互联网科技有限公司
丰台科技园 互联网公司
互联网公司会议桌创意科技软装饰
海南科技产业互联网平台
成都互联网科技公司100强
和信科技工业互联网
互联网科技与大数据专业
电子科技大学互联网创业大赛
乌苏新华互联网科技做什么的
陕西九四连强互联网科技有限公司
恒生科技指数 中概互联网
山东蛋芒互联网科技有限公司
网互联网络科技
六子台科技互联网展会
伟宏互联网科技
北京互联网络科技有限公司
互联网科技logo设计
无锡溥仪互联网科技有限公司
互联网5g黑科技有哪些
洛阳共同富裕互联网科技有限公司
广州千川互联网科技有限公司
互联网营销推广黑科技
珠海东科互联网科技有限公司
云鲸互联网络科技有限公司