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:
handle.write("#
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, for your use case "print the name if it has o
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
handle.name. Either of those might also emit AttributeE
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" % name)
Ya that would
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 accesses
>> handle.name. Either of those migh
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
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
> probably not be sile
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 you leap” (LBYL) versus “ea
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 another?
try/except is mo
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/
--
https://mail.python.org/ma
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 Attr
11 matches
Mail list logo