Re: Try: rather than if :

2015-12-15 Thread jmp
On 12/14/2015 11:38 PM, Vincent Davis wrote: In the code below try is used to check if handle has the attribute name. It seems an if statement could be used. Is there reason one way would be better than another? def write_header(self): handle = self.handle try:

Re: Try: rather than if :

2015-12-14 Thread Chris Angelico
On Tue, Dec 15, 2015 at 10:48 AM, Vincent Davis wrote: > try: > write = handel.write > except AttributeError: > raise Just "write = handel.write" :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Re: Try: rather than if :

2015-12-14 Thread Vincent Davis
On Mon, Dec 14, 2015 at 4:14 PM, Cameron Simpson wrote: > First, notice that the code inside the try/except _only_ fetches the > attribute. Your version calls the "write" attribute, and also accesses > handle.name. Either of those might also emit AttributeError, and should >

Re: Try: rather than if :

2015-12-14 Thread Ian Kelly
On Mon, Dec 14, 2015 at 4:48 PM, Vincent Davis wrote: > On Mon, Dec 14, 2015 at 4:14 PM, Cameron Simpson wrote: > >> First, notice that the code inside the try/except _only_ fetches the >> attribute. Your version calls the "write" attribute, and also

Re: Try: rather than if :

2015-12-14 Thread Vincent Davis
On Mon, Dec 14, 2015 at 4:53 PM, Ian Kelly wrote: > > Except that catching an exception just to immediately re-raise it is > silly. This would be better: > > try: > name = handle.name > except AttributeError: > pass > else: > handle.write("# Report_file: %s\n"

Re: Try: rather than if :

2015-12-14 Thread Cameron Simpson
On 15Dec2015 17:11, Cameron Simpson wrote: On 14Dec2015 16:48, Vincent Davis wrote: [...] ​I think the intent of the original code was to check if handle had the attribute "name", I don't think the attribute "write" was the issue. [...] Secondly,

Re: Try: rather than if :

2015-12-14 Thread Cameron Simpson
On 14Dec2015 16:48, Vincent Davis wrote: On Mon, Dec 14, 2015 at 4:14 PM, Cameron Simpson wrote: First, notice that the code inside the try/except _only_ fetches the attribute. Your version calls the "write" attribute, and also accesses

Try: rather than if :

2015-12-14 Thread Vincent Davis
In the code below try is used to check if handle has the attribute name. It seems an if statement could be used. Is there reason one way would be better than another? def write_header(self): handle = self.handle try: handle.write("# Report_file: %s\n" % handle.name) except

Re: Try: rather than if :

2015-12-14 Thread Ian Kelly
On Mon, Dec 14, 2015 at 3:38 PM, Vincent Davis wrote: > In the code below try is used to check if handle has the attribute name. It > seems an if statement could be used. Is there reason one way would be > better than another? http://www.oranlooney.com/lbyl-vs-eafp/ --

Re: Try: rather than if :

2015-12-14 Thread Cameron Simpson
On 14Dec2015 15:38, Vincent Davis wrote: In the code below try is used to check if handle has the attribute name. It seems an if statement could be used. Only by using hasattr(), which IIRC does a try/except internally. Is there reason one way would be better than

EAFP and LBYL (was: Try: rather than if :)

2015-12-14 Thread Ben Finney
Vincent Davis writes: > In the code below try is used to check if handle has the attribute name. It > seems an if statement could be used. Is there reason one way would be > better than another? The Python community refers to the difference by contrasting “look before