0

Below, I print out the html for a form defined externally. Is there a difference in the way each string is retrieved and used in foo.py, other than the syntax? If so, in what circumstances would one method be preferred over the other? For example, would I be better off defining a number of html files in a module as strings and access them that way, as opposed keeping them in separate .html files and using open over and over?

mod.py

form = """\
<form type="POST" action="test.py">
   Enter something:<input type="text" name="somethign">
</form>
"""

form.html

<form type="POST" action="test.py">
   Enter something:<input type="text" name="something">
</form>

foo.py

import mod

print mod.form

with open('form.html', 'r') as form:
   print form.read()
3
  • There is the slight overhead of opening and closing files; but it will only come into play if you are opening a very large number of files. Commented Oct 16, 2012 at 15:30
  • The Zen of Python: import this Commented Oct 16, 2012 at 15:38
  • OK, but is the import exactly == to a single file open? and are there any new processes being run when I access parts of the module that might be equivalent to opening a new file for example? I'm trying to establish the difference in mechanics between the two methods. Commented Oct 16, 2012 at 15:39

2 Answers 2

1

Having .html files is better. Sure, you will have some overhead opening a file, reading its content and then closing it, but you'll have many advantages:

  • .html file can be edited by any person who knows HTML syntax.
  • .html files can be edited without restarting your program, it is very useful for services.
  • You can eliminate open/read/close overhead by introducing some caching technique.
Sign up to request clarification or add additional context in comments.

Comments

1

It's a lot easier for designers to edit discrete HTML files than to deal with HTML embedded in code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.