[Edu-sig] Re: Python for CS101

Scott David Daniels Scott.Daniels at Acm.Org
Sat Apr 30 19:42:17 CEST 2005


Douglas S. Blank wrote:

In response to FAQ 5:

 > 5. What's the system for doing explicit type checking? We already have
 > the students write preconditions for functions, so even a way of just
 > asking "what is the type of value in this variable?" and then
 > comparing it to some other type, in an assertion, would be fine.
 > But my impression is that there's something more organized than this.

You should mention:
     assert isinstance(someValue, SomeBaseClass)
and even:
     assert isinstance(someValue, (int, long, float, complex))

 > Python has assert(expression). You can get a variable's type with
 > the type() function. But you could also build a run-time type
 > checking system like:

 > @typeChecker
 > def studentDefineFunction(x, y, z):
 >     tempValue = x + y * z
 >     return tempValue

 > where typeChecker is something the instructor could write to run-time
 > check the input and return types (and even values).

Also, in your response, I'd not call the decorator typeChecker, but
simply "checker."  Since the whole idea of decorators is a bit strange,
you'd at least need to explain that this allows you to wrap the student
function and watch its inputs and outputs "in situ."  Personally, I'd
avoid type checking until a way in to the course.


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Edu-sig mailing list