Hi!

I have a class:

class A(object):
  calledMethod = None
  def a1(self):
    pass
  def a2(self):
    pass

Now when I create an object and call some method:

a = A()
a.a1()
#or
a.a2()

I need somehow to save the name of called method in variable calledMethod. Could this be done with descriptors? That will be the best way since I dont want to put extra code in every method to find its name...

Thanks

This is odd. Each method already knows its own name. For example:

class A(object):

  def a1(self):
      self.calledMethod = "a1"

  def a2(self):
      self.calledMethod = "a2"

That is, when you call a function or method, the instruction pointer knows which code block it's currently in.

So what is it that you want to do with these names?

Confused,
Jeff

Hi!

Well, that's exactly the solution that I was trying to avoid. I don't want to put code in every method to assign that method name to calledMethod variable. Let's say that I have a 30 different classes where each of them can have many methods. In that case I'll have to put in every single method an assignment to variable calledMethod. That seems to me like a waste of code. I would like a solution where every class would have a same parent class with some code(descriptors?) which can track every method call and put called method name into variable calledMethod.
Anyway thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.