[Tutor] How do I do this in python?

spir denis.spir at free.fr
Thu Jun 11 13:45:00 CEST 2009


Le Thu, 11 Jun 2009 13:12:02 +0200,
"A.T.Hofkamp" <a.t.hofkamp at tue.nl> s'exprima ainsi:

> > If this can't be done but there is a completely different way to
> > achieve a similar result, what is it?
> 
> print "abc=", a, b, c
> 
> is what I always use.

Well, I do find the OP's demand really sensible. Had the same need (and still have), esp. for everyday quick debugging. Strangely enough, I also called this show() and had a need for a kind of "polyShow()", and for a "treeShow()", too...

Actually, python (only) knows the name. There is no implementation issue, I guess: I would love a builtin (can only be builtin) func, or better a statement, to do that. May accept only name(s) and raise NameError on wrong call.

Then, we could insert in code lines like
   show(count, result)
instead of eg
   print "count:%s, result:%s" %(count,result)

A statement version may be:
   show count, result
or why not
   ? count, result

In the case of a statement, it's much more consistent to require the parameter list to contain only names.
This would really make my life nicer. It's *so* stupid to be obliged to repeat the name! And even more when you think python knows it!! And even more when you realise _you_ have no way to know it, for most objects don't have a __name__ attribute!!!

By the way, for funcs, bound & unbound methods, classes/types, modules, you can use this attr:

=======
def show(*args):
    for arg in args:
        try:
            print "%s:%r" %(arg.__name__,arg)
        except AttributeError:
            print "?:%r" % arg
def f():pass
n=1
show(f,n)
==> output ==>
f:<function f at 0xb7e76f7c>
?:1
=======

But this does not bring much, sure...

Denis
------
la vita e estrany


More information about the Tutor mailing list