0

I'm writing a SOAP web service for Django which will have a lot of functions. To keep it clean, I would like to split it in several files.

How do I "include" my functions code into my class (like in PHP).

For example:

class SOAPService(ServiceBase):
  #first file
  from myfunctions1 import *
  #second file
  from myfunctionsA import *

Should behave like:

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

   ...

2 Answers 2

1

Python lets you inherit from multiple classes, so go ahead an put your methods into a separate base class and inherit from it, too.

Also, python lets you pull in from other files with the import statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Please see my comment on Andrew's answer about mixing classes.
0

Although there are several ways to do this, the first one I would recommend is to create mixin classes from both files.

So in your main module

import myfunctions1
import myfunctionsA
class SOAPService(ServiceBase,myfunctions1.mixin,myfunctionsA.mixin):
    pass

and in your included modules myfunctions1, myfunctionA etc.

class mixin:
    def myfunction1(self):
        return
    def myfunction2(self, arg1):
        return

2 Comments

The problem is, according to this comment, that I'm not supposed to mix ServiceBase with other classes stackoverflow.com/questions/24268065/…
In this case I think it is ok. I didn't see anything in ServiceBase that would cause this mixin style to break.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.