Special Methods

Certain methods have special meaning to the interpreter

  • These are denoted with leading and trailing underscores (__)
  • There are a few dozen special methods than can be defined.

__init__

  • Called to initialize a newly created instance.
     def __init__(self,initial):
          self.balance = initial

__del__

  • Called before an instance is about to be destroyed.
     def __del__(self):
          print "Ay!!!! I'm being killed" 
  • Note: it is rarely necessary to define __del__
  • Only needed if you need to perform cleanup actions (e.g., close a network connection)
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 95
July 17, 2000, beazley@cs.uchicago.edu
>>>