Need help with Python class idioms

Mel Wilson mwilson at the-wire.com
Thu Jan 22 13:05:40 EST 2004


In article <mailman.613.1074720629.12720.python-list at python.org>,
James Henderson <james at logicalprogression.net> wrote:
>On Wednesday 21 January 2004 9:01 pm, Jonathan Daugherty wrote:
>> # P.S. Since you're asking about idiom, it would be more Pythonic to write:
>> #
>> #     if self.mandatoryVar1 is None:
>> #
>> # using the identity test "is" rather than equality test "==", since there
>> is # only one None.
>>
>> I'd write
>>
>>   if not self.mandatoryVar1:
>>
>> but the first method works, too.
>
>Are you sure an empty sequence or zero aren't valid values?  J.

   It can get weird if you have to allow your caller to
optionally specify the value None:


    class _MissingParam:
        "An object that no-one else will have a valid use for."
...
    def a_func (opt1=_MissingParam):
        if opt1 is _MissingParam:
            opt1 = default_calculation ()       # for instance
...


        Regards.        Mel.



More information about the Python-list mailing list