How can I programmatically find the name of a method from within that method?

Tony aclarke11 at yahoo.co.uk
Wed Aug 8 16:05:50 EDT 2007


On Aug 8, 8:25 am, Peter Otten <__pete... at web.de> wrote:
> kj7ny wrote:
> > What is @checkPrivs (see example copied below from other post)?  In
> > fact... how does the thing work at all?
> > @checkPrivs
> > def add(a,b):
> >     return a+b
>
> @... is called a decorator and is just a fancy way of writing
>
> def add(a, b):
>     return a+b
> add = checkPrivs(add)
>
> Peter

Is this cheating?

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




More information about the Python-list mailing list