Reputation: 3282
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 function 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"
Upvotes: 7
Views: 26324
Reputation: 88
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()
Upvotes: 1
Reputation: 141810
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.
Upvotes: 1
Reputation: 17893
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.
Upvotes: 2
Reputation: 17321
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()
Upvotes: 2
Reputation: 319601
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.
Upvotes: 8
Reputation: 38346
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()
Upvotes: 1