[Tutor] confusion about imports
Chris Hare
chare at labr.net
Tue Jul 10 03:18:31 CEST 2012
Good advice - thanks for that. And I think you're right - I think what is happening is in fact a bunch of circular references. As I resolve issues, I will be looking for those! Appreciate all the advice!
On Jul 9, 2012, at 5:16 PM, Dave Angel wrote:
> 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
>
More information about the Tutor
mailing list