On 22 nov, 14:05, Ivan Sagalaev <[EMAIL PROTECTED]> wrote:
> Chris wrote:
> > try:
> >     push = getattr(self, 'get_%s_content' % self.name)
> > except AttributeError:
> >     raise "Method does not exist"
> > if callable(push):
> >     push(**argv)
>
> There are a couple of problems with this code.
>
> 1. It's more readable to call hasattr than to catch an exception from
> getattr:

Actually, most python programmer will favor the getattr solution, at
least if they expect the given attribute to exists as "the normal
case".

> if hasattr(self, 'get_%s_content' % self.name):
>      raise Exception('Method does not exists')

logical bug above.

> if callable(push):

NameError here.

>      push(**argv)


> 2. Raising another exception instead of the standard AttributeError
> looks suspicious. Why are you doing it?
>
> 3. Exceptions should be instances of class Exception, not strings.
>
> 4. The code silently does nothing when attribute is here but is not
> callable.

Agree on these 3 points.


--~--~---------~--~----~------------~-------~--~----~
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