python meta_class_experiment.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python meta_class_experiment.py相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# coding: utf-8


class MetaHandler(type):
    def __new__(mcs, name, bases, attrs):
        instance = type.__new__(mcs, name, bases, attrs)
        if hasattr(instance, 'get'):
            sub_get = getattr(instance, 'get')

            def get(self):
                sub_get(self)
                print('I am static get')

            setattr(instance, 'get', get)
        return instance


class BaseHandler(object):
    __metaclass__ = MetaHandler


# noinspection PyMethodMayBeStatic,PyMethodMayBeStatic
class SubHandler(BaseHandler):
    def get(self):
        print 'SubHandler.get'


def main():
    """
    :return:
    SubHandler.get
    I am static get
    """
    sh = SubHandler()
    sh.get()


if __name__ == '__main__':
    main()

以上是关于python meta_class_experiment.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识