[英]How to include class from another file(in another folder) - python
[英]Include functions in class from another file in python
我正在为 Django 编写一个 SOAP Web 服务,它将有很多功能。 为了保持干净,我想把它分成几个文件。
我如何将我的函数代码“包含”到我的类中(就像在 PHP 中一样)。
例如:
class SOAPService(ServiceBase):
#first file
from myfunctions1 import *
#second file
from myfunctionsA import *
应该表现得像:
class SOAPService(ServiceBase):
#first file
def myfunction1(ctx):
return
def myfunction2(ctx, arg1):
return
#second file
def myfunctionA(ctx):
return
def myfunctionB(ctx, arg1):
return
...
Python 允许您从多个类继承,因此继续将您的方法放入一个单独的基类并从中继承。
此外,python 允许您使用 import 语句从其他文件中提取。
尽管有多种方法可以做到这一点,但我推荐的第一种方法是从两个文件创建mixin类。
所以在你的主模块中
import myfunctions1
import myfunctionsA
class SOAPService(ServiceBase,myfunctions1.mixin,myfunctionsA.mixin):
pass
并在您包含的模块 myfunctions1、myfunctionA 等中。
class mixin:
def myfunction1(self):
return
def myfunction2(self, arg1):
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.