开发者

importing class and its function from another file

开发者 https://www.devze.com 2023-01-01 13:21 出处:网络
I am having little problem with importing classes in python. My work flow goes like this index.py class Template:

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

index.py

    class Template:

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

display.py

I want to call func开发者_如何转开发tion header(), body() and footer () in my display.py page. Will anyone make me clear about this issue in python. Thanks for your concern.

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

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


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

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.


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.

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


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:

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

See how Body() gets called twice?

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.


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:

from index import template

foo = template()

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


You have the following code at the top and the bottom of 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.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号