Coming from C, I think that it's generally a good programming practice
to make sure everything you create, closes; whether it's about a socket
or a file. This may not be the case with Python though. To be honest,
leaving this task to the garbage collector doesn't sound like a good
idea to me (since
Good point, but with your way, if "s = ... # socket opens" fails, then
nothing will catch it. What I usually do is what I wrote above (place
it below the 2nd try), and when attempting to close it, first use an if
like: "if locals().has_key('s'):".
--
http://mail.python.org/mailman/listinfo/python
I need it because the "various code" may raise other exceptions (not
related to sockets). In such case, the "except socket.error, x:" won't
catch it, but thanks to the "finally:", it is sure that the socket will
close.
--
http://mail.python.org/mailman/listinfo/python-list
Thanks. This should now be OK:
#try:
#try:
#s = ... # socket opens
#
## various code ...
#except socket.error, x:
## exception handling
#finally:
#s.close() # socket closes
--
http://mail.python.org/mailman/listinfo/python-list
(I don't know why, but indentation was not preserved once I posted.)
--
http://mail.python.org/mailman/listinfo/python-list
Hello.
In a program, I want to ensure that a socket closes (so I use try ...
finally), but I also want to catch/handle a socket exception. This is
what I have done:
try:
try:
s = ... # socket opens
# various code ...
except socket.error, x:
# exception handling
finally:
s.close() # socket closes
OK, the symbolic link solved the "problem". I thought that there was
something wrong with my Python configuration; that's why I asked in the
first place.
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Hello.
Under Gentoo Linux, I issue:
$ python timeit.py
python: can't open file 'timeit.py'
$ ls -al /usr/lib/python2.3/timeit.py
-rw-r--r-- 1 root root 9833 Oct 19 02:17 /usr/lib/python2.3/timeit.py
But if I specify the full path, it works:
$ python /usr/lib/python2.3/timeit.py -n 1 "pass"
1 lo
Yes, I could do the timing myself. Sorry if this was impolite -- it was
not in my intentions. The main reason I asked was about the reason.
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Any idea which of the following is faster?
'a/b/c/'[:-1]
or
'a/b/c/'.rstrip('/')
Thanks in advance.
P.S. I could time it but I thought of trying my luck here first, in
case someone knows already, and of course the reason.
--
http://mail.python.org/mailman/listinfo/python-list
10 matches
Mail list logo