[Tutor] Explain to me Initializing

Steven D'Aprano steve at pearwood.info
Sun Jan 16 02:01:19 CET 2011


walter weston wrote:
> Please explain to me what initializing is,like when you _init_ a function or class . what is _init_ and what does it really do ? do you need it?


Do you know how it is when you get in a car, and turn the key in the 
ignition, and change into gear, and only then the car is ready to drive 
forward? That's initializing.

As for whether you need to initialize or not, that depends on whether 
you're driving a car or pedaling a push-bike. Not everything needs 
special steps before it is ready to use.


In Python, when you create a new instance, *if* the class defines 
__init__, then it will be run to perform the initialization. If you 
don't need one, don't bother with a pointless "do nothing" __init__.


-- 
Steven


More information about the Tutor mailing list