Re: del an imported Class at EOF... why?



Carl Banks wrote:
On Oct 6, 10:56 am, Ryan <heni...@xxxxxxxxx> 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

.



Relevant Pages

  • Re: Object creation overhead
    ... one particular use is to reduce the memory ... 'MyClass'; this class looks like: ... 20,000 'String' objects is needed, both to manage them, and for the 'String' ... The 'proxy' class approach won't see a reduction in the number of 'MyClass' ...
    (comp.lang.java)
  • Re: C# confusion
    ... If you create instances of MyClass, ... you are allocating an area of memory for the data segment for each ... data members are initialised upon first reference of the class. ... static keyword; This one has been hitting me like a hammer on the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# confusion
    ... MyClass" then you will never create an instance of MyClass on the heap, ... so any instance members you declare in MyClass will never exist. ... of memory: not the stack and not the heap, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to terminate an object thats in a collection
    ... Dim C As Collection ... Dim tmp As Class1 ... My memory is pretty clear on this, but it is not clear on whether there ... Coll.Remove 1 'removes item from collection, but MyClass still exists ...
    (microsoft.public.excel.programming)
  • Re: PHP vs. Python
    ... >less memory than PHP and do not need to parse so many lines. ... Not that I'm disagreeing with your overall premise here, that Python ... bound to names in the current namespace. ... The performance advantages of importing only one or two ...
    (comp.lang.python)

Loading