Call method from another Python file

As I am learning Django / Python right now, I have not yet used the concept of classes. As far as I know, this method is not static. This is just a standard definition.

So, let's say I have this package called Example1c views.pywhich contains this method:

def adder(x,y):
  return x + y

Then I have one Example2, which also has views.pywhere I would like to use this method adder.

How can I do it?

EDIT: in Java, it will be a simple instance, and then instantiation.Method(), or if it was static, it will SomeClass.Method(), however, I'm not sure how I should approach this in Python.

+5
source share
3 answers

Python . "" - , import Name().

from Example1.views import adder as otherAdder

. otherAdder(), . , Example1.views, , .

+7

Try:

from Example2.views import adder as myadder
myadder(x,y)

, python, dir , .

: 'as'

# use ...views as myviews if you need to avoid name conflict on views
from Example2 import views
views.adder(x,y)
+6

""

:

from Example2 import views as views2

x = views2.adder(1, 2)

, , , -)

+3

All Articles