What is the difference between init and enter?

J

John

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.
 
D

Diez B. Roggisch

John said:
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
 
L

Lie Ryan

Diez said:
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
 
C

Carl Banks

The proper response to a question like this has to be
   http://www.catb.org/~esr/faqs/smart-questions.html
as anything else is complete guesswork.

Is there a Cliff's Notes version of this?

I may be a cynic but I would think the people who ask bad questions
are the same sort of people who won't have the patience to sit though
this behemoth of an essay.

And I may be a romantic but I would think most people who post this
are really trying to be helpful and not just saying, "Out of my midst,
vile newbie, until thou hearkenst unto the sacred words".

So, it would seem that a summary (with citations) of the essay would
be useful.


Carl Banks
 
A

Aahz

Is there a Cliff's Notes version of this?

I may be a cynic but I would think the people who ask bad questions
are the same sort of people who won't have the patience to sit though
this behemoth of an essay.

Perhaps linking directly to this would help:

http://www.catb.org/~esr/faqs/smart-questions.html#before

The table of contents contains a good summary for actually asking
questions. Overall, I'm inclined to agree with people who argue that the
smart questions essay is a good filter; someone unwilling to put in the
work of reading and understanding it is also unwilling to put in the
effort to ask good questions. I mean, I re-read most of that essay once
every couple of years myself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top