6

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"

3
  • 5
    I recommend you to read up on more Python tutorials. Plenty of examples out there. =] Commented Jun 1, 2010 at 10:46
  • 1
    Also, please note that the Python style guide (python.org/dev/peps/pep-0008) recommends using CapWords in class names, not all lowercase letters. Commented Jun 1, 2010 at 10:51
  • 1
    Thanks for the code! we couldn't have figured it out otherwise. Now go accept more answers. =3 Commented Jun 1, 2010 at 11:38

6 Answers 6

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried the same like u did. but i always get repetition like 2 times body() etc
@user: you're probably calling methods from the Template's __init__. Otherwise, there is nothing in this code that would produce duplicate.
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.

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

Comments

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:

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.

9 Comments

yeah every function has self parameter which im aware of
What other problems do you have? Can you provide us with more code?
Like my workflow above, i have index.py file [with given class and fucntions] and from index.py file i want to pass values from form() to display.py. In display.py i need to call only header(), body() and footer(). But i am doing same things like u guys said, but got repetition in the display page. Display page goes like this- header(), body(), form(), footer() and again body() and some other cotent from display page.
@user: we need to see the actual code of your template class and its methods. We're not psychics.
@user343934: If you call body() twice, then surely you expect the output of body() twice...
|
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:

from index import template foo = template() foo.header() foo.body() foo.footer() 

2 Comments

there's a couple of typos here, that render your answer completely nonsensical.
@Sil: The first revision of my answer was indeed quite buggy. If it was not for the annoying "are you human" feature, the typos would have been fixed before your comment :)
1

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.

3 Comments

It seems working but still with one error that in display.py i receive "Content-type: text/html " on the top
Yes... that code is pulled in with the module too. I've included that in my revised answer.
@user343934: Yeah, because you should have put the content type also into a HTML-proper syntax.
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() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.