What is the difference between init and enter?
lie.1296 at gmail.com
Tue May 26 15:23:43 CEST 2009
Diez B. Roggisch wrote:
> John wrote:
>> I'm okay with init, but it seems to me that enter is redundant since it
>> appears that anything you want to execute in enter can be done in init.
> About what are you talking?
Do you mean __init__ and __enter__?
They are used for two completely different purpose. __init__ is used to
initialize an object when they've just been created. __enter__ is called
when an object enters a Context Manager (i.e. with statement)
Example, when opening a file:
# this calls file.__init__
f = open('foo.bar')
# some codes...
# this calls file.__enter__
More information about the Python-list