Carl Banks wrote:
On Oct 6, 10:56 am, Ryan <heni...@yahoo.com> wrote:
Good day all!

I've just inherited a large amount of python code. After spending some
time pour through the code, I've noticed that the original developer
(who is no longer w/ the company) constantly deletes the imported
classes at the end of the .py file. Why would one want to do such a
thing?


Sometimes an object is used only temporarily by a modules, while it's
importing.

In such cases, there are two reasons you might delete the object.  If
it uses a lot of memory you could free up the memory for other
objects.  Also you might want to clean up the module's namespace.

I del objects for these reasons sparingly, usually only when the
object uses a whole lot of memory, or the namespace is very messy.

However, I'm guessing that the person who wrote your code is just
overly paranoid.



Carl Banks

There are several things you might have meant by "the end of the .py file" Presumably you mean in top-level code, and since you said module and not script, I'm presuming this is the code that runs outside of the if __name__ == logic.

An example would be very good.  I'd assume you meant something like:

//start module
from extern_module import MyClass

some definitions and classes

some common initialization code

if __name__ == "__main__":
     some testing code

del MyClass
//end module

In other words, these classes are being deleted before the module is made visible to other modules that imported it. This seems to me just prudent management. He/she is making sure that the importer of your code doesn't use your code as a longcut to get at MyClass. He's forcing them to import them explicitly (from extern_module), not just get your copy. Presumably these classes are not a part of your public interface, so they shouldn't be visible, except with a leading underscore. Naturally, if you try to use them directly in your own definitions and classes, you'll also have trouble. So presumably these are classes that are used only in the initialization code, or as base classes for new classes defined in your code, or in default value expressions of your function definitions.

I tend to always use the plain "import xmodule" form of statement, so just one symbol gets into my space. And each use of a class from there is of the form xmodule.MyClass

Clearly there you would not delete MyClass in your own code, though you could delete xmodule.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to