1

In "Think Python: How to Think Like a Computer Scientist", the author defines postcondition as:

A requirement that should be satisfied by the function before it ends.

He also states:

Conversely, conditions at the end of the function are postconditions. Postconditions include the intended effect of the function (like drawing line segments) and any side effects (like moving the Turtle or making other changes).

So assume that we have a function called factorial that has a required parameter called n, isn't the expected postcondition of it that it must (i.e it is required to) return a positive integer that represents the product of numbers from 1 through n? Isn't this requirement satisfied after factorial ends?

Is this definition right?

Would defining postcondition as "A requirement that should be satisfied by the function after it ends." be right?

Note: I'm a beginner in programming, in general, and Python, in particular.

3 Answers3

1

A postcondition is "a requirement that must be true at the moment a function ends", i.e.: At the exact moment the function ends, and nothing more has happened, the postcondition of the function must be true.

The definition in your book is actually somewhat consistent with this: If the postcondition is satisfied by the function before it ends, and the function doesn't do anything that would render the condition false, then of course the postcondition will be true at the moment the function ends.

Your definition is also consistent with this, in that right after the function ends, its postcondition must be true.

I think the main issue here is the definition of the word "satisfy". If we take "to satisfy a condition" to mean "to make that condition true" (which seems to be the definition your book uses) then a postcondition must become true at some point while the function runs and before it returns so that it may be true at the moment the function's execution ends. If you take "satisfy" to mean "to have the condition be true" (which seems to be how you are using the word), then your definiton makes sense - immediately after the function ends, its postcondition must be true.

Semantics!

Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
  • Thank you, Pedro. What is the difference between "to make that condition true" and "to have the condition be true"? – Mahmood Muhammad Nageeb Apr 20 '17 at 19:23
  • 1
    "To make the condition true" would mean that the condition was false before, and some action changes it into being true. This use of "satisfy" expresses an action which changes the truth value of the condition. "To have the condition be true" would simply imply that the condition is already satisfied, with no action involved. – Pedro Castilho Apr 20 '17 at 19:25
1

For what it's worth, in Python anything that can happen in a function on that particular branch of execution must happen before the functions ends:

In [6]: import dis

In [7]: def fun():
   ...:     return 42
   ...:

In [8]: dis.dis(fun)
  2           0 LOAD_CONST               1 (42)
              2 RETURN_VALUE

In the simple case, just 42, the constant is loaded.

But what about something a little more complicated, like a try with a return inside?

In [9]: def fun_with_exceptions():
   ...:     try:
   ...:         return 42
   ...:     finally:
   ...:         print('Returning')
   ...:

Turns out:

In [11]: dis.dis(fun_with_exceptions)
  2           0 SETUP_FINALLY            4 (to 6)

  3           2 LOAD_CONST               1 (42)
              4 RETURN_VALUE

  5     >>    6 LOAD_GLOBAL              0 (print)
              8 LOAD_CONST               2 ('Returning')
             10 CALL_FUNCTION            1
             12 POP_TOP
             14 END_FINALLY
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE

All of the things in the finally still execute before the function actually returns - so the definition of postcondition as

A requirement that should be satisfied by the function before it ends.

Is still totally valid.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
0

The definition "A requirement that should be satisfied by the function before it ends." is correct.

Consider the function create_logger(file_name) it takes a string parameter and file_name, returns a file stream for the given file_name which can be used to write log messages.

In this case post condition is that returned object be a writable file stream (main objective of the function).
Additionally it might also ensure that is clears any previous file with identical name (house-keeping/clean-up activity).
And there is enough space/permission to write to newly created file (sanity check).
Post condition can be created for both the main objective and sanity checks. which will become true at some point during the execution of the function and before it returns.

However it is not necessary that these condition remain True after the function returns. Hence "A requirement that should be satisfied by the function after it ends." is incorrect.
For example at some later point after the function return the disk might fill up and returned file stream object may not be writable any more.

shanmuga
  • 4,329
  • 2
  • 21
  • 35