Paul McNett wrote: > Ricardo Aráoz wrote: >> Paul McNett wrote: >>> Ricardo Aráoz wrote: >>>> Paul McNett wrote: >>>>> Actually, you'd be better off checking that you have an object first: >>>>> >>>>> lbl = getattr(self, curlabel, None) >>>>> if lbl: >>>>> lbl.Caption = "somestr" >>>>> >>>>> HTH >>>>> >>>> How un-pythonic! Use exceptions! >>> Ok: >>> >>> try: >>> lbl = getattr(self, curlabel) >>> except AttributeError: >>> lbl = None >>> >>> if lbl: >>> lbl.Caption = "somestr" >>> >>> Please explain why those 6 lines are better than my original 3. >>> >> try : lbl = getattr(self, curlabel) >> except: # do other stuff your original code won't do > > Well, actually, I could have added an else: if I needed that... > > >> finally : lbl.Caption = "somestr" >> >> I guess those three lines do what your original 3 do, but faster. > > Faster? In this case, I believe it would tie. Exceptions when raised are > not good performance-wise. However, I believe that the getattr() call > will cause an AttributeError if the attribute doesn't exist (which it > catches and snuffs out if you provide a default value, or re-raises if > not). So exceptions are being raised in either case, resulting in a tie > performance-wise. >
Note : I tried this >>> boogey = list() >>> lbl = getattr(boogey, curlabel) Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'curlabel' is not defined I guess the error raised by getattr would be NameError. I didn't use a named error in my code just in case :) _______________________________________________ Post Messages to: [email protected] Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users Searchable Archives: http://leafe.com/archives/search/dabo-users This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]
