[Tutor] class methods: using class vars as args?

Steven D'Aprano steve at pearwood.info
Sat May 29 03:01:10 CEST 2010


On Fri, 28 May 2010 07:42:30 am Alex Hall wrote:
> Thanks for all the explanations, everyone. This does make sense, and
> I am now using the
> if(arg==None): arg=self.arg
> idea. It only adds a couple lines, and is, if anything, more explicit
> than what I was doing before.

You should use "if arg is None" rather than an equality test.

In this case, you are using None as a sentinel value. That is, you want 
your test to pass only if you actually receive None as an argument, not 
merely something that is equal to None.

Using "arg is None" as the test clearly indicates your intention:

The value None, and no other value, is the sentinel triggering special 
behaviour

while the equality test is potentially subject to false positives, e.g. 
if somebody calls your code but passes it something like this:

class EqualsEverything:
    def __eq__(self, other):
        return True

instead of None.



-- 
Steven D'Aprano


More information about the Tutor mailing list