Saturday, September 1, 2012

Re: What does this code do? Also, testing decorator via terminal?

On 1-9-2012 8:38, Kurtis Mullins wrote:
>>
>> On Fri, Aug 31, 2012 at 9:17 PM, Micky Hulse <rgmicky@gmail.com> wrote:
>>> I'd like to add:
>>> if objects.status_code != 200:
>>> return objects
>>> ... which is found via this code:
>>> <https://gist.github.com/871954>
>>
>> Doh! After a bit of trial and error, I given up on trying to merge
>> those two code snippets.
>>
>> I'd still like to know what `if isinstance(objects, HttpResponse)` is
>> doing?
>
>
> All that is doing is checking to see if the object named "objects" is an
> HttpResponse object.

More precise, if it's an object instance that has HttpResponse as a
class in it's inheritance chain.
The goal of this check is to be sure you can perform certain operations
on it. You can of course just perform these operations and if the
operation isn't supported then just let it blow up, but doing an
isinstance check allows you to catch the error earlier and give a better
diagnostic to the end user.
There's also a negative to the check: If I have good reasons not to
subclass HttpResponse but can still make it behave like HttpResponse,
then this check is working against me.

--
Melvyn Sopacua

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment