Unification of Methods and Functions

Greg Ewing greg at cosc.canterbury.ac.nz
Sun May 23 22:07:20 EDT 2004


David MacQuigg wrote:
> import math
> 
> def mypow(x, y):
>       return x**y
> 
> class MathA:
>    pow = math.pow
> 
> class MathB:
>    pow = mypow
> 
> ma = MathA()
> mb = MathB()
> 
> print ma.pow(2,4) #=>
> 16.0
> print mb.pow(2,4) #=>
> # TypeError: mypow() takes exactly 2 arguments (3 given)
> 
> How would you explain this to non-CIS students in a class on circuit
> design, where there is very little time to discuss programming?

I wouldn't have to, because I would never show them any code
that did anything as screwy as this in the first place, and
it's extremely unlikely they would stumble upon it themselves
in their first few hours of using Python.

To put it another way, this is not the sort of thing you
do in the normal course of writing Python code. It's an
example contrived specifically to be confusing, not the
sort of thing you'd use in a teaching course.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list