6
63q2o4i02
Hi, I was wondering how I may get a python function to know what its
name is without me having to write it manually? For example:
def func1():
<do some stuff1>
print 'func1'
return True
def func2():
<do some stuff2>
print 'func2'
return True
should be more like
def func1():
<do some stuff 1>
print <self-name>
return True
def func2():
<do some stuff 2>
print <self-name>
return True
I imagine this means things like closures which I'm not familiar with
(I'm not a CS person). In this case, each function is part of a class,
so I imagine I can take a dir() of the class if necessary.
This leads into my next related question, which is How do I get some
sort of macro behavior so I don't have to write the same thing over and
over again, but which is also not neatly rolled up into a function,
such as combining the return statements with a printing of <self-name>?
My application has a bunch of functions that must do different things,
then print out their names, and then each call another function before
returning. I'd like to have the last function call and the return in
one statement, because if I forget to manually type it in, things get
messed up.
(ok, I'm writing a parser and I keep track of the call level with a tab
count, which gets printed before any text messages. So each text
message has a tab count in accordance with how far down the parser is.
Each time a grammar rule is entered or returned from, the tab count
goes up or down. If I mess up and forget to call tabsup() or tabsdn(),
the printing gets messed up. There are a lot of simple cheesy
production rules, [I'm doing this largely as an exercise for myself,
which is why I'm doing this parsing manually], so it's error-prone and
tedious to type tabsup() each time I enter a function, and tabsdn()
each time I return from a function, which may be from several different
flow branches.)
Thanks for any help
Michael
name is without me having to write it manually? For example:
def func1():
<do some stuff1>
print 'func1'
return True
def func2():
<do some stuff2>
print 'func2'
return True
should be more like
def func1():
<do some stuff 1>
print <self-name>
return True
def func2():
<do some stuff 2>
print <self-name>
return True
I imagine this means things like closures which I'm not familiar with
(I'm not a CS person). In this case, each function is part of a class,
so I imagine I can take a dir() of the class if necessary.
This leads into my next related question, which is How do I get some
sort of macro behavior so I don't have to write the same thing over and
over again, but which is also not neatly rolled up into a function,
such as combining the return statements with a printing of <self-name>?
My application has a bunch of functions that must do different things,
then print out their names, and then each call another function before
returning. I'd like to have the last function call and the return in
one statement, because if I forget to manually type it in, things get
messed up.
(ok, I'm writing a parser and I keep track of the call level with a tab
count, which gets printed before any text messages. So each text
message has a tab count in accordance with how far down the parser is.
Each time a grammar rule is entered or returned from, the tab count
goes up or down. If I mess up and forget to call tabsup() or tabsdn(),
the printing gets messed up. There are a lot of simple cheesy
production rules, [I'm doing this largely as an exercise for myself,
which is why I'm doing this parsing manually], so it's error-prone and
tedious to type tabsup() each time I enter a function, and tabsdn()
each time I return from a function, which may be from several different
flow branches.)
Thanks for any help
Michael