从另一个文件导入类及其功能

时间:2021-02-14 16:00:37

I am having little problem with importing classes in python. My work flow goes like this

我在python中导入类没什么问题。我的工作流程是这样的

index.py

    class Template:

        def header():
        def body():
        def form():
        def footer():

display.py

I want to call function header(), body() and footer () in my display.py page. Will anyone make me clear about this issue in python. Thanks for your concern.

我想在display.py页面中调用函数header(),body()和footer()。有人会在python中让我清楚这个问题。感谢你的关心。

Index file--- [Index.py][1]

索引文件--- [Index.py] [1]

[1]: http://pastebin.com/qNB53KTE and display.py -- "http://pastebin.com/vRsJumzq"

[1]:http://pastebin.com/qNB53KTE和display.py - “http://pastebin.com/vRsJumzq”

6 个解决方案

#1


2  

At the bottom of your index file you create a HtmlTemplate object and call all the methods on it. Since this code is not contained in any other block, it gets executed when you import the module. You either need to remove it or check to see if the file is being run from the command line.

在索引文件的底部,您可以创建一个HtmlTemplate对象并调用其上的所有方法。由于此代码不包含在任何其他块中,因此在导入模块时会执行该代码。您需要将其删除或检查文件是否正在从命令行运行。

if __name__ == "__main__":
    objx=HtmlTemplate()
    objx.Header()
    objx.Body()
    objx.Form()
    objx.Footer()
    objx.CloseHtml()

#2


7  

What have you tried? The following would be normal way of using methods of Template class after import.

你尝试过什么?以下是导入后使用Template类方法的常规方法。

from index import Template

t = Template()
t.header()
t.body()
t.footer()

ETA: at the end of your index.py file (lines 99-105) you're calling all the functions from the above-defined Template class. That's why you're seeing duplicates.

ETA:在index.py文件的末尾(第99-105行),您将调用上面定义的Template类中的所有函数。这就是你看到重复的原因。

#3


2  

Edit: Okay, I see what your problem is, given your code.

编辑:好的,我看到你的问题是什么,给出你的代码。

You're calling the following:

你打电话给以下人:

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
objx.Header()
objx.Body()
objx.Form()
objx.Footer()
objx.CloseHtml()

And then in your display.py:

然后在你的display.py中:

t = HtmlTemplate()
t.Header()
t.Body()

See how Body() gets called twice?

看看Body()被调用两次?

As a footnote, you should use lowercase for method names, and Capital words for classes as you're doing now. It's a good convention. I greatly recommend it.

作为脚注,您应该使用小写作为方法名称,使用大写单词作为现在正在进行的类。这是一个很好的约定。我非常推荐它。

You should simply construct the object once in display.py and call all the methods.

您应该只在display.py中构造一次对象并调用所有方法。

#4


1  

I am not sure if I understand you correctly, but I believe you are asking how to import the template class in another script. The import statement is what you need:

我不确定我是否理解正确,但我相信您正在询问如何在另一个脚本中导入模板类。 import语句是您需要的:

from index import template

foo = template()

foo.header()
foo.body()
foo.footer()

#5


1  

You have the following code at the top and the bottom of index.py:

您在index.py的顶部和底部有以下代码:

cgitb.enable()
print 'Content-type: text/html\n\n'
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"

# [...]

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
# [...]      
objx.CloseHtml()

This will be called each time you import index.

每次导入索引时都会调用此方法。

To prevent this happening, put it in a block thus:

为了防止这种情况发生,请将其放在一个块中:

if __name__ == '__main__':
    cgitb.enable()
    print 'Content-type: text/html\n\n'
    print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"

    # [...]

    ## Calling all the functions of the class template with object (objx)
    objx=HtmlTemplate()
    # [...]      
    objx.CloseHtml()

...or better still put this code functions that can be called from elsewhere.

...或者更好的是仍然可以从其他地方调用此代码函数。

#6


1  

The below solution worked for me:

以下解决方案对我有用:

class1(unittest.TestCase):
   def method1(self)

class2(unittest.TestCase):
   def method2(self):
      instance_name = class1("method1")
      instance_name.method1()

#1


2  

At the bottom of your index file you create a HtmlTemplate object and call all the methods on it. Since this code is not contained in any other block, it gets executed when you import the module. You either need to remove it or check to see if the file is being run from the command line.

在索引文件的底部,您可以创建一个HtmlTemplate对象并调用其上的所有方法。由于此代码不包含在任何其他块中,因此在导入模块时会执行该代码。您需要将其删除或检查文件是否正在从命令行运行。

if __name__ == "__main__":
    objx=HtmlTemplate()
    objx.Header()
    objx.Body()
    objx.Form()
    objx.Footer()
    objx.CloseHtml()

#2


7  

What have you tried? The following would be normal way of using methods of Template class after import.

你尝试过什么?以下是导入后使用Template类方法的常规方法。

from index import Template

t = Template()
t.header()
t.body()
t.footer()

ETA: at the end of your index.py file (lines 99-105) you're calling all the functions from the above-defined Template class. That's why you're seeing duplicates.

ETA:在index.py文件的末尾(第99-105行),您将调用上面定义的Template类中的所有函数。这就是你看到重复的原因。

#3


2  

Edit: Okay, I see what your problem is, given your code.

编辑:好的,我看到你的问题是什么,给出你的代码。

You're calling the following:

你打电话给以下人:

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
objx.Header()
objx.Body()
objx.Form()
objx.Footer()
objx.CloseHtml()

And then in your display.py:

然后在你的display.py中:

t = HtmlTemplate()
t.Header()
t.Body()

See how Body() gets called twice?

看看Body()被调用两次?

As a footnote, you should use lowercase for method names, and Capital words for classes as you're doing now. It's a good convention. I greatly recommend it.

作为脚注,您应该使用小写作为方法名称,使用大写单词作为现在正在进行的类。这是一个很好的约定。我非常推荐它。

You should simply construct the object once in display.py and call all the methods.

您应该只在display.py中构造一次对象并调用所有方法。

#4


1  

I am not sure if I understand you correctly, but I believe you are asking how to import the template class in another script. The import statement is what you need:

我不确定我是否理解正确,但我相信您正在询问如何在另一个脚本中导入模板类。 import语句是您需要的:

from index import template

foo = template()

foo.header()
foo.body()
foo.footer()

#5


1  

You have the following code at the top and the bottom of index.py:

您在index.py的顶部和底部有以下代码:

cgitb.enable()
print 'Content-type: text/html\n\n'
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"

# [...]

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
# [...]      
objx.CloseHtml()

This will be called each time you import index.

每次导入索引时都会调用此方法。

To prevent this happening, put it in a block thus:

为了防止这种情况发生,请将其放在一个块中:

if __name__ == '__main__':
    cgitb.enable()
    print 'Content-type: text/html\n\n'
    print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"

    # [...]

    ## Calling all the functions of the class template with object (objx)
    objx=HtmlTemplate()
    # [...]      
    objx.CloseHtml()

...or better still put this code functions that can be called from elsewhere.

...或者更好的是仍然可以从其他地方调用此代码函数。

#6


1  

The below solution worked for me:

以下解决方案对我有用:

class1(unittest.TestCase):
   def method1(self)

class2(unittest.TestCase):
   def method2(self):
      instance_name = class1("method1")
      instance_name.method1()