Closed GoogleCodeExporter closed 8 years ago
I have considered doing this, only because the context manager documentation
made
such a big deal out of it, but I first have to ask why.
You can already do this:
def x():
cnxn = pyodbc.connect(...)
# do something
return row.count
Python's C implementation uses reference counting, so as long as you didn't
stash a
reference to the connection anywhere, it goes out of scope and is closed
automatically. Same for a cursor. This works when exceptions are thrown or if
you
exit normally.
Obviously you aren't going to be using pyodbc with Jython, so that can't be an
issue.
I guess I can see wanting to close a connection in the middle of a function,
but you
can just put cnxn=None and your done. Generally you don't have to worry about
exceptions if they escape the function since that will close your connection
too.
The only scenario I've come up with so far is:
def func():
try:
cnxn = ...
except:
...
morecodehere()
If you want the connection closed before morecodehere and you don't want to put
in
two cnxn.close() or cnxn = None statements. I've written a lot of code with
pyodbc
and I haven't hit this yet.
I'm not opposed to adding this feature, but can you provide a use case?
Original comment by mkleehammer
on 2 Jan 2009 at 4:43
I agree with you that this isn't necessarily needed. My thought was that it
would be
nice to have some consistency across APIs when working with external resources.
I
think it would be nice as a developer to know that when working with files
(a.k.a.
the "open" function) and database connections, I could use the "with - as"
syntax and
guarantee closure of these resources. I think this would be easier for new
Python
devs to grasp and would lead to potentially better code. New Python devs may
not be
aware of reference counting... just a thought. I could be totally wrong. :)
Original comment by brian.br...@gmail.com
on 2 Jan 2009 at 6:36
I do like consistency, but I would think it should go the other direction. I
personally never use the with statement because it has been unnecessary for me.
In
fact, I'm concerned it was requested by developers who don't understand how the
reference counting works.
As for new developers, not having to put try/finally everywhere is one of the
attractive features I've Python, IMHO. I almost don't want new developers to
discover the with statement until they have a solid understanding of the way
Python
works.
Let me continue to hold this for a while. Perhaps we should try starting a
discussion on the groups?
Original comment by mkleehammer
on 2 Jan 2009 at 11:02
typo above: I've Python --> of Python
Original comment by mkleehammer
on 2 Jan 2009 at 11:15
Original comment by mkleehammer
on 24 Jan 2009 at 3:57
Original issue reported on code.google.com by
brian.br...@gmail.com
on 2 Jan 2009 at 4:33