• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

示范python如何创建和使用一个简单的元类

python 水墨上仙 2535次浏览

演示python如何创建和使用一个简单的元类

#!/usr/bin/env python
#-*- coding: utf-8-*-
# [代码名字: Simple metaclass]
# [代码分类: Python Core] 
# [代码描述: Shows how to create a and use a simple metaclass]
# [代码作者: Florian Diesch <diesch@spamfence.net>]
# [SNIPPET_DOCS: http://www.ibm.com/developerworks/linux/library/l-pymeta.html]
# [代码协议: MIT]
# Copyright 2010 Florian Diesch <diesch@spamfence.net>
# 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 setter
class 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如何创建和使用一个简单的元类
喜欢 (0)
加载中……