Re: How can I programmatically find the name of a method from within that method?
- From: Peter Otten <__peter__@xxxxxx>
- Date: Wed, 08 Aug 2007 22:28:59 +0200
Tony wrote:
Is this cheating?
Isn't it harder to calculate the magic indices than just writing down the
names twice?
class a:
def square(self, x):
print 'executing:', dir(self)[-1]
print x*x
def cube(self, x):
print 'executing:', dir(self)[-2]
print x*x*x
b=a()
b.square(3)
b.cube(3)
Output:
PyMate r6780 running Python 2.3.5 (python)
function self naming2.py
executing: square
9
executing: cube
27
Is this cheating?
No, just wrong.
.... def alpha(self): return dir(self)[-2]class A:
.... def gamma(self): return dir(self)[-1]
....
('alpha', 'gamma')a = A()
a.alpha(), a.gamma()
('beta', 'gamma')a.beta = 42
a.alpha(), a.gamma()
Peter
.
- Follow-Ups:
- References:
- How can I programmatically find the name of a method from within that method?
- From: kj7ny
- Re: How can I programmatically find the name of a method from within that method?
- From: Jay Loden
- Re: How can I programmatically find the name of a method from within that method?
- From: kj7ny
- Re: How can I programmatically find the name of a method from within that method?
- From: Peter Otten
- Re: How can I programmatically find the name of a method from within that method?
- From: Tony
- How can I programmatically find the name of a method from within that method?
- Prev by Date: Re: Issues of state (was: Tkinter or wxpython?)
- Next by Date: Re: Misleading wikipedia article on Python 3?
- Previous by thread: Re: How can I programmatically find the name of a method from within that method?
- Next by thread: Re: How can I programmatically find the name of a method from within that method?
- Index(es):
Relevant Pages
|