Python meta-programming (meta-class instance)

Source: Internet
Author: User

This instance is a meta-class instance, and the function is to record the class name of the subclass, and to display the class name of the subclass in a tree-like structure.

Registerclasses inherits from type, which provides the ability to create a collection of childrens for a class in the __init__ interface, and to save the class name to the collection of childrens of the corresponding parent class tuple.

The __str__ printing method and the __iter__ iterative method are also defined, in which:

The __iter__ method returns the childrens collection of the class name and enters the elements therein.

While Sharp inherits from Registerclasses, when for s in Sharp:print S, the inherited __iter__ method is called.

Class Round (Shape): Pass
Class Square (Shape): Pass
Class triangular (Shape): Pass
Class Boxy (Shape): Pass

In the above operation, Round,square,triangular,boxy Initializes a collection of childrens,
At the same time, it adds its own class name to the collection of childrens of the shape meta-class.

Class Circle (Round): Pass
Class Ellipse (Round): Pass

In the above operation, Circle,ellipse Initializes a collection of childrens,
At the same time, all of their class names are added to the Round,shape childrens collection,

The reason: Round's parent class is shape, so Circle,ellipse has two parent classes.

The code is as follows:
#!/usr/bin/env python
# Encoding:utf-8
"""
@author:
@contract:
@file: homework5_1.py
@time: 2016/10/26 14:22
"""
Class Registerclasses (Type):
Def __init__ (CLS, name, bases, Atts):
Super (Registerclasses, CLS). __INIT__ (name, bases, Atts)

#创建一个集合, so inheriting the meta-class, there will be a collection of childrens
Cls.childrens = set ()

#将把当前的子类保存到父类中去
For base in bases:
If Hasattr (base, ' childrens '):
Base.childrens.add (CLS)

#classmethod, called on class object
def __iter__ (CLS):
Return iter (Cls.childrens)

def __str__ (CLS):
If Len (Cls.childrens) > 0:
return cls.__name__ + ":" + ",". Join ([sc.__name__ for SC in CLS])
Else
Return cls.__name__

Class Shape (object):
__metaclass__ = registerclasses

Print "---------------------"
Class Round (Shape): Pass
Class Square (Shape): Pass
Class triangular (Shape): Pass
Class Boxy (Shape): Pass
Print Shape
Print "---------------------"
Class Circle (Round): Pass
Class Ellipse (Round): Pass
Print Shape
Print "---------------------"
For S in Shape: #Iterator over subclasses (Def __str__ (CLS):)
Print S
Print "---------------------"
For CLS in Shape.childrens:
If Len (Cls.childrens) > 0:
Print cls.__name__ + ":" + ",". Join ([sc.__name__ for SC in CLS])
Else
Print cls.__name__

The output is as follows:

---------------------
Shape:triangular, Boxy, Square, Round
---------------------
Shape:triangular, Boxy, Square, Round
---------------------
Triangular
Boxy
Square
Round:ellipse, Circle
---------------------
Triangular
Boxy
Square
Round:ellipse, Circle




Python meta-programming (meta-class instance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.