[Tutor] class and methods/functions

w chun wescpy at gmail.com
Sat Oct 22 23:16:35 CEST 2005


On 10/22/05, Jacob S. <keridee at jayco.net> wrote:
> >>   def nameCheck(self, value):
> >> >         import re
> >> >         tempREG = re.match('.*:.*',value)
> >> >         return str(tempREG) != 'None'
>
> This could be written better.
>
> def nameCheck(self, value):
>     tempREG = re.match('.*:.*',value)
>     return tempREG != None
>
> There's no need to convert to string. None is a type that you can check
> against directly.

and even more so, a further refinement takes advantage of a single None value:

return tempREG is not None

this works faster than "tempREG != None" because this is an object
*value* comparison (vs an object *identity* comparison).  in value
comparisons, the interpreter has to pull out the "value" of both
objects and compare them.  for identity checks, it just checks the
objects themselves.  this extra step saves time and improves
performance.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list