top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

opposite of __init__.py in python

+1 vote
932 views

Is there is a opposite of __init__.py like __del__.py

I want, that when the application ends, certain functions are executed. I know I could make a constructor and a destructor, but I simply want to know if there is a opposite....

posted Aug 19, 2013 by Jagan Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Yes, check the atexit module:
http://docs.python.org/3/library/atexit.html

answer Aug 19, 2013 by Majula Joshi
+1 vote

No. If you want to run code when your application is shutting down, run it just before your code finishes:

def main():
 do_this()
 do_that()

if __name__ == '__main__':
 main()
 shutdown()

If you care about code running even if an exception takes place:

if __name__ == '__main__':
 try:
 main()
 finally:
 shutdown()
answer Aug 19, 2013 by Mandeep Sehgal
Similar Questions
+1 vote

I want to install path.py in my Python 3.4 environment on a Centos 5 box. My /usr/local/bin/ contains:

easy_install-3.4 
python3.4  
etc. 

We are behind a proxy server and I tried this:

# /usr/local/bin/easy_install-3.4 path.py 

Searching for path.py 
Reading https://pypi.python.org/simple/path.py/ 
Download error on https://pypi.python.org/simple/path.py/: hostname '172.29.68.1  
' doesn't match either of 'www.python.org', 'python.org', 'pypi.python.org',  
'docs.python.org', 'testpypi.python.org', 'bugs.python.org', 'wiki.python.org',  
'hg.python.org', 'mail.python.org', 'packaging.python.org', 'pythonhosted.org',  
'www.pythonhosted.org', 'test.pythonhosted.org', 'us.pycon.org', 'id.python.org' --  
Some packages may not be found! 

Couldn't find index page for 'path.py' (maybe misspelled?), Am I best to use pip or easy_install? also if easy_install, how can I fix the above error?

+1 vote

This code is from The Python Cookbook, 2nd edition, 12.2 Counting Tags in a Document:

from xml.sax.handler import ContentHandler
import xml.sax
class countHandler(ContentHandler):
 def __init__(self):
 self.tags={}
 def startElement(self, name, attr):
 self.tags[name] = 1 + self.tags.get(name, 0)

Isn't overriding __init__ a risky thing to do? The docs don't mention it as a method I should override, and also don't define what's in there or if I'd need to call the base class __init__. Moreover, startDocument is provided for parser setup.

As it happens, ContentHandler.__init__ isn't empty, so the above code could fail if the parser isn't prepared for _locator to be undefined. Is the above code is an acceptable idiom?

+2 votes

I have an application which uses extensively python 2.7.6 (CPython).

The issue that I see is the following: "If there are only pyc files, the loading time of the application is much more than if I have pyc and py files. It is behind with 2 minutes more than if it had py files"

Do you have any idea why this is happening?

+1 vote

I've dome some reading on the difference between __new__ and __init__, and never really groked it. I just followed the advice that you should almost always use __init__.

I recently came across a task that required using __new__ and not __init__. I was a bit intimidated at first, but it was quick and easy. This simple programming exercise really cleared a lot of things up for me.

Not to be immodest, but I think something like this ought to be the canonical example for explaining when/how to override __new__.

The task? I want to make a class that behaves exactly like a tuple, except changing the constructor argument signature and adding some extra methods. An example should clarify what I needed.

> x = ParetoTuple(1, 2, 0)
> x[1]
>> 2
> len(x)
>> 3
> 2 in x
>> True
> -1 in x
>> False
> x.dominates(ParetoTuple(1, 3, 0))
>> True
> x.equivalent(ParetoTuple(1, 2 + 1e-5, 0))
>> True

etc.

Since I want the constructor to take an (almost) arbitrary number of arguments, each of which will be elements of the resulting ParetoTuple, I need to override __new__. I don't need to overwrite __init__, because the tuple.__new__ will populate it's data when the arguments are properly formatted.

Also, since the world of Pareto comparisons makes sense only with 2 or more goals, I want my specialized constructor to take at least 2 arguments in a natural way.

Here is the code

class ParetoTuple(tuple) :
 def __new__ (cls, obj1, obj2, *rest):
 return super(ParetoTuple, cls).__new__(cls, (obj1, obj2) + rest)
 # nothing special about the dominates, equivalents methods...
 # no __init__ needed

I understand some people argue in favor of using a factory pattern for this sort of situation, but I disagree. I think the cognitive overhead of factories requires a more complicated task than re-signaturing the constructor method.

...