从不同的Python文件调用方法

时间:2022-08-08 16:03:42

As I'm currently learning Django / Python, I've not really been using the concept of Classes, yet. As far as I'm aware the method isn't static.. it's just a standard definition.

由于我目前正在学习Django / Python,我还没有真正使用过Classes的概念。据我所知,该方法不是静态的......它只是一个标准的定义。

So let's say I have this Package called Example1 with a views.py that contains this method:

所以,假设我有一个名为Example1的Package,其中包含一个包含此方法的views.py:

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

Then I have an Example2 which also has a views.py where I'd like to use this method adder.

然后我有一个Example2,它也有一个views.py,我想使用这个方法加法器。

How would I got about doing this?

我怎么会这样做?

EDIT: In Java it would be a simple instantiation then a instantiation.Method() or if it was static it would be SomeClass.Method() however I'm unsure how I should approach this in Python.

编辑:在Java中它将是一个简单的实例化,然后是instantiation.Method()或如果它是静态的,它将是SomeClass.Method()但是我不确定我应该如何在Python中处理它。

3 个解决方案

#1


7  

Python has module level and class level methods. In this concept a "module" is a very special class that you get by using import instead of Name(). Try

Python具有模块级和类级方法。在这个概念中,“模块”是一个非常特殊的类,可以使用import而不是Name()来获得。尝试

from Example1.views import adder as otherAdder

to get access to the module level method. Now you can call otherAdder() and it will execute the code in the other module. Note that the method will be executed in the context of Example1.views, i.e. when it references things, it will look there.

访问模块级方法。现在你可以调用otherAdder(),它将执行另一个模块中的代码。请注意,该方法将在Example1.views的上下文中执行,即当它引用事物时,它将在那里查看。

#2


6  

Try:

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

If you are unsure, you can always start a python shell and then use the dir command to look inside the contents of packages to see what can be used.

如果您不确定,可以随时启动python shell,然后使用dir命令查看包的内容以查看可以使用的内容。

Edit: Updated from comments to use 'as'

编辑:从评论更新以使用'as'

OR

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

#3


3  

Normally you import "modules"

通常你导入“模块”

I would suggest:

我会建议:

from Example2 import views as views2

x = views2.adder(1, 2)

I hope, I got this right, since I did not use packages till now ;-)

我希望,我做对了,因为我到现在才使用包装;-)

#1


7  

Python has module level and class level methods. In this concept a "module" is a very special class that you get by using import instead of Name(). Try

Python具有模块级和类级方法。在这个概念中,“模块”是一个非常特殊的类,可以使用import而不是Name()来获得。尝试

from Example1.views import adder as otherAdder

to get access to the module level method. Now you can call otherAdder() and it will execute the code in the other module. Note that the method will be executed in the context of Example1.views, i.e. when it references things, it will look there.

访问模块级方法。现在你可以调用otherAdder(),它将执行另一个模块中的代码。请注意,该方法将在Example1.views的上下文中执行,即当它引用事物时,它将在那里查看。

#2


6  

Try:

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

If you are unsure, you can always start a python shell and then use the dir command to look inside the contents of packages to see what can be used.

如果您不确定,可以随时启动python shell,然后使用dir命令查看包的内容以查看可以使用的内容。

Edit: Updated from comments to use 'as'

编辑:从评论更新以使用'as'

OR

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

#3


3  

Normally you import "modules"

通常你导入“模块”

I would suggest:

我会建议:

from Example2 import views as views2

x = views2.adder(1, 2)

I hope, I got this right, since I did not use packages till now ;-)

我希望,我做对了,因为我到现在才使用包装;-)