Is there an include file for class statements in python?
I think this is almost certainly a very basic question but I'm struggling开发者_运维技巧 with it. I have 2 python files, one for the main content on my site and one for the admin area. You can see the two files here:
https://gist.github.com/670034
In both these files I define the classes and import the necessary modules at the top and if I change in one file I need to remember to change in the other file. Is there a way to define all these in one file and then include this for both files?
I'm sure there is an easy way of doing this but I can't seem to find how to do it.
Thanks
Tom
Just create a file, like myimports.py
:
import cgi
import os
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.datastore import entity_pb
Then you can do:
from myimports import *
Note that while this saves space, it doesn't make it more readable as you need to check the additional file just to see what you imported.
Echoing what poke says, it's maybe better not to make your imports in a centralised place as you can end up losing fine grained control over what is in your namespace. I know this does not answer your question, but it may help in the long run.
Create new module:
import some
class A:
pass
and import module in the both files:
import new_module
Define the classes in a separate file and import
it in the other two.
精彩评论