Newbie tip: Prg speed from ~5 hrs to < 1 minute

Geoff Gerrietts geoff at gerrietts.net
Mon Apr 29 14:03:40 EDT 2002


The use of "pass" in the example doesn't actually do anything. I think
what you're looking for is "continue". I can't explain the dramatic
speedup you got out of adding them. It defies logic.

The "pass" keyword is a no-op. It's a placeholder. The idea is that
you might want to do something like this:

class Spam:
  pass

In this example, "pass" is a way to indicate that you don't want
anything special -- your Spam class can use a default constructor, and
needs no special methods. But you can't just write:

class Spam:

That's a syntax error. So "pass" allows you to say "do nothing
special". This is also a way you can insert a virtual method:

class Spam:
  def subclassed_operation(self):
    pass

But "pass" isn't always the preferred approach. Lots of people have
commented that better form might be:

class Spam:
  def subclassed_operation(self):
    """ this operation must be overridden by the subclass if it is to
        do anything.
    """

The doc string counts as a "statement" for the purposes of the
compiler, making this valid syntax. Meanwhile, it provides explicit
explanation of your intent.

Still others will prefer both: have a doc string AND a pass.

The "continue" keyword says "let's go back to the top of the loop and
do the next item". The "break" keyword says "let's exit the loop right
now, even if there's more work pending".

Those are your loop-control statements.

Luck,
--G.


-- 
Geoff Gerrietts             "If life were measured by accomplishments, 
<geoff at gerrietts net>     most of us would die in infancy." 
http://www.gerrietts.net/       --A.P. Gouthey





More information about the Python-list mailing list