What is the difference between init and enter?
Lie Ryan
lie.1296 at gmail.com
Tue May 26 09:23:43 EDT 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?
>
> Diez
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__
with f:
pass
More information about the Python-list
mailing list