getattr is definitely preferable to self.__class__.__dict__

--Ned.
http://nedbatchelder.com

Henry Andrews wrote:
> I've aways been under the impression that direct use of __dict__ was
> discouraged.  Something along the lines of __dict__ being an
> implementation detail while getattr and hasattr are the proper
> interfaces.  But I might be wrong.
>
> thanks,
> -henry
>
> On Nov 22, 4:07 am, "Steve McConville" <[EMAIL PROTECTED]>
> wrote:
>   
>> try:
>>     self.__class__.__dict__[method_name](self, argument)
>> except KeyError:
>>     #handle missing method
>>     pass
>>
>> is pretty much equivalent to
>>
>> try:
>>     getattr(self, method_name)(argument)
>> except AttributeError:
>>     # handle missing method
>>     pass
>>
>> Don't know if anyone would consider it more elegant but it doesn't use
>> getattr :)
>>
>> --
>> steevhttp://www.daikaiju.org.uk/~steve/
>>     
>
> >
>
>
>   

-- 
Ned Batchelder, http://nedbatchelder.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to