Which idiom is better? try: ... except Attr..Err or hasattr?
Gerrit Holl
gerrit.holl at pobox.com
Thu Jan 27 09:10:22 EST 2000
Peter Funk wrote on 948958807:
> Look at the following coding alternatives often seen in generic python code:
>
> class A:
> def load(self):
> # ... do something e.g. load a file
> try:
> self.update()
> except AttributeError:
> pass
>
> class B:
> def load(self):
> # ... do something e.g. load a file
> if hasattr(self, 'update'):
> self.update()
>
> Which coding idiom is better? The second form is one line shorter than
> the first, but it has the disadvantage, that the method (or attribute) must
> be named twice. Both idioms are used in the standard libary.
The second one: is self.update() contains an error raising an
AttributeError, you don't want to catch it.
regards,
Gerrit.
--
Please correct any bad English you encounter in my email message!
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK-----
More information about the Python-list
mailing list