"Compile time" checking?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Aug 11 21:52:11 CEST 2005

In <1123708379.036298.134210 at g43g2000cwa.googlegroups.com>, Qopit wrote:

> I'm
> a big fan of Python's ability to easily rebind everything in sight, but
> this particular usage seems like a strange abuse I wouldn't expect a
> code-checker to be able to figure out.  I'll just avoid writing
> confusing code like that... it's not only confusing to a program, but
> to a human as well!  Dynamically massacring a function definition (as
> opposed to just rebinding a new implementation) like that seems odd to
> me.

Well it's a contrived example.  But taking a function or method and wrap
it with some other function or method isn't that uncommon.  For methods
there is even syntactic sugar: decorator syntax.

Here's a more useful dynamic function wrapping::

 def debug_trace(func):
     def wrapper(*args, **kwargs):
         print 'Calling %s' % func.__name__
         print 'args: %r' % (args,)
         print 'kwargs: %r' % kwargs
         func(*args, **kwargs)
     return wrapper

 def test(a, b, c):
     print 'just a test', a, b, b

 test = debug_trace(test)

 test(1, 2, 3)
 test(1, 2)

Here it's quite clear to a human reader that the wrapping doesn't change
the number of arguments.  But it's harder to let a program figure this out.

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list