On 07/09/2012 11:56 AM, Chris Hare wrote: > So, I have to admit, imports have me really confused. I am trying to break > apart a 10,000+ line single file into various files, one for each class, and > one containing a whole bunch of functions which are used by a lot of classes. > Some of those functions use calls to methods in a Class. Even though the > Class has been imported, I get a nameError where trying to use the class. I > have read about Classes and packages and modules, but import just has me > confused. Something I haven't seen explicitly mentioned in this thread is that when you make those modules to hold classes DO NOT make the module name the same as the class name.
If you have a class MyClass defined in your 10k file, and you want to move it to a separate file, and if you really wanted to dedicate the file to a single class, then the file might be called myclass.py and the references to it would like something like: import myclass ... obj = myclass.MyClass(arg1, arg2) or alternatively, from myclass import MyClass ... obj = MyClass(arg1, arg2) You wouldn't believe how much confusion people get into when they have module names that look like class names. Another thing is that you probably want several related classes and functions in each module. This is not Java. At that point, you might want from people import MyFirstPeopleClass, MySecondPeopleClass, peoplefunction Finally, upon more careful reading of your original query, you may have circular dependencies happening here. A function may use class methods, and class methods may use the function. But if both are true, then put them in the same module. Having one module import a second one which imports the first is an invitation to disaster. And a special place in debugging hell is reserved for those that try to import the script that invokes it all. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor