2

我想运行一个类似这样的命令,但该函数handle (self,*args,**options)似乎没有执行嵌套函数。

如何在其中包含我的功能handle()

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    def handle(self, *args, **options):
        def hello():
            print "hello1"
        def hello1():
            print "hello2"
4

1 回答 1

3

您还可以“动态”定义函数:

class Command(NoArgsCommand):
    def handle_noargs(self):
        def hello():
            print "hello1"

        def hello1():
            print "hello2"

        hello()
        hello1()

或在命令之外(因为它们在“服务”起作用):

def hello():
    print "hello1"

def hello1():
    print "hello2"


class Command(NoArgsCommand):

    def handle_noargs(self):
        hello()
        hello1()
于 2012-04-20T08:55:20.523 回答