[Pulp-list] Interesting python inspection

Jeff Ortel jortel at redhat.com
Thu Aug 19 20:54:09 UTC 2010



On 08/19/2010 12:23 PM, Pradeep Kilambi wrote:
> On 08/19/2010 12:55 PM, Jason Dobies wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> On 08/19/2010 12:55 PM, Jason Rist wrote:
>>> On 08/19/2010 10:28 AM, Jason Dobies wrote:
>>>> PyCharm just flagged the following code with a warning (in particular
>>>> the consumerids argument)
>>>>
>>>> def create(self, id, description, consumerids=[]):
>>>>
>>>> Here's the warning:
>>>>
>>>> "This inspection detects when a mutable value as list or dictionary is
>>>> detected in a default value for an argument. Default argument values
>>>> are
>>>> evaluated only once at function definition time, which means that
>>>> modifying the default value of the argument will affect all subsequent
>>>> calls of the function. "
>>>>
>>>> That's kinda cool, I never saw anything like that before.
>>>>
>>> It knows...
>>> _______________________________________________
>>> Pulp-list mailing list
>>> Pulp-list at redhat.com
>>> https://www.redhat.com/mailman/listinfo/pulp-list
>>>
>>> _______________________________________________
>>> Pulp-list mailing list
>>> Pulp-list at redhat.com
>>> https://www.redhat.com/mailman/listinfo/pulp-list
>> Something else I keep seeing in the code is:
>>
>> if foo == None:
>>
>> It's better to just use object reference comparison rather than equality
>> when checking for none:
>>
>> if foo is None:
>
> you don't necessarily need 'is None or == None'. You can jus do:
>
> if not foo:
>
> and that will automatically conclude the same check.

Be careful doing this.  If foo implements __len__() like: (list, tuple, str, ...) and it 
returns 0, testing as:

if not foo:
   ...

will evaluate to True when the intent is testing for (None).

Taking this into account, testing as:

if foo:
   ...

-or-

if not foo:
   ...

Is handy when you don't need to differentiate between foo=None or foo being empty 
collection or string.  Eg: code may want to treat an empty string the same as None.


>
>
> _______________________________________________
> Pulp-list mailing list
> Pulp-list at redhat.com
> https://www.redhat.com/mailman/listinfo/pulp-list

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5126 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://listman.redhat.com/archives/pulp-list/attachments/20100819/5b5b17d5/attachment.p7s>


More information about the Pulp-list mailing list