這篇文章給大家分享的是有關(guān)python怎么創(chuàng)建和使用一個(gè)簡(jiǎn)單的元類的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
目前創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、盧氏網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
示例
#!/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 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
感謝各位的閱讀!關(guān)于“python怎么創(chuàng)建和使用一個(gè)簡(jiǎn)單的元類”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!