Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python create and use a simple metaclass

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how python creates and uses a simple metaclass. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Example

#! / usr/bin/env python# [SNIPPET_NAME: Simple metaclass] # [SNIPPET_CATEGORIES: Python Core] # [SNIPPET_DESCRIPTION: Shows how to create an 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 aclass 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% slots% (book.title) Book.author) book.author = 'Euclid' book.title =' Elements' print'% s by% slots% (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 Thank you for reading! This is the end of this article on "how to create and use a simple metaclass in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report