Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用修饰的方法派生类?_Python_Oop_Jinja2_Cherrypy - Fatal编程技术网

Python 如何用修饰的方法派生类?

Python 如何用修饰的方法派生类?,python,oop,jinja2,cherrypy,Python,Oop,Jinja2,Cherrypy,我有几个类似的python cherrypy应用程序 应用程序_one.py import cherrypy class Class(object): @cherrypy.tools.jinja(a='a', b='b') @cherrypy.expose def index(self): return { 'c': 'c' } import cherrypy class Class(object):

我有几个类似的python cherrypy应用程序

应用程序_one.py

import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a', b='b')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c'
        }
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a2', b='b2')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c2'
        } 
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='aN', b='bN')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'cN'
        }
import cherrypy

class ParentClass(object):

    _a = None
    _b = None
    _c = None

    @cherrypy.tools.jinja(a=self._a, b=self._b)
    @cherrypy.expose
    def index(self):
        return {
            'c': self._c
        }
import parent

class Class(ParentClass):

    _a = 'a'
    _b = 'b'
    _c = 'c'
应用程序\u two.py

import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a', b='b')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c'
        }
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a2', b='b2')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c2'
        } 
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='aN', b='bN')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'cN'
        }
import cherrypy

class ParentClass(object):

    _a = None
    _b = None
    _c = None

    @cherrypy.tools.jinja(a=self._a, b=self._b)
    @cherrypy.expose
    def index(self):
        return {
            'c': self._c
        }
import parent

class Class(ParentClass):

    _a = 'a'
    _b = 'b'
    _c = 'c'

应用程序\u n.py

import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a', b='b')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c'
        }
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a2', b='b2')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c2'
        } 
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='aN', b='bN')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'cN'
        }
import cherrypy

class ParentClass(object):

    _a = None
    _b = None
    _c = None

    @cherrypy.tools.jinja(a=self._a, b=self._b)
    @cherrypy.expose
    def index(self):
        return {
            'c': self._c
        }
import parent

class Class(ParentClass):

    _a = 'a'
    _b = 'b'
    _c = 'c'
我想创建父类并在所有应用程序中派生它。 像这样的

parent.py

import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a', b='b')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c'
        }
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a2', b='b2')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c2'
        } 
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='aN', b='bN')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'cN'
        }
import cherrypy

class ParentClass(object):

    _a = None
    _b = None
    _c = None

    @cherrypy.tools.jinja(a=self._a, b=self._b)
    @cherrypy.expose
    def index(self):
        return {
            'c': self._c
        }
import parent

class Class(ParentClass):

    _a = 'a'
    _b = 'b'
    _c = 'c'
应用程序_one.py

import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a', b='b')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c'
        }
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='a2', b='b2')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'c2'
        } 
import cherrypy

class Class(object):

    @cherrypy.tools.jinja(a='aN', b='bN')
    @cherrypy.expose
    def index(self):
        return {
            'c': 'cN'
        }
import cherrypy

class ParentClass(object):

    _a = None
    _b = None
    _c = None

    @cherrypy.tools.jinja(a=self._a, b=self._b)
    @cherrypy.expose
    def index(self):
        return {
            'c': self._c
        }
import parent

class Class(ParentClass):

    _a = 'a'
    _b = 'b'
    _c = 'c'
应用程序2.py

import parent

class Class(ParentClass):

    _a = 'a2'
    _b = 'b2'
    _c = 'c2'
如何从派生类发送索引方法装饰器的参数

现在我收到错误信息

NameError:未定义名称“self”

定义类时会应用装饰器。定义类时,您没有运行方法,因此未定义
self
。没有可参考的
self
实例

您必须使用元类,在构建子类时添加decorator,或者必须使用类decorator,在定义类后应用正确的decorator

类装饰器可以是:

def add_decorated_index(cls):
    @cherrypy.tools.jinja(a=cls._a, b=cls._b)
    @cherrypy.expose
    def index(self):
        return {
            'c': self._c
        }

    cls.index = index
    return cls
然后将其应用于子类:

import parent

@parent.add_decorated_index
class Class(parent.ParentClass):
    _a = 'a'
    _b = 'b'
    _c = 'c'