Function Names on a function call

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indiarocks
    New Member
    • Feb 2007
    • 20

    #1

    Function Names on a function call

    Let's say for eg. I have one common function say 'common_func' and it can be called by any other functions. If func1, makes a call to common_func, I need to determine in common_func that this call was made by func1. If func2 makes a call to common_func, that this call was made by func2.

    I need a way to figure out which function made a function call to common_func.

    I don't know if I am making sense.

    Thanks,
    -V
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by indiarocks
    Let's say for eg. I have one common function say 'common_func' and it can be called by any other functions. If func1, makes a call to common_func, I need to determine in common_func that this call was made by func1. If func2 makes a call to common_func, that this call was made by func2.

    I need a way to figure out which function made a function call to common_func.

    I don't know if I am making sense.

    Thanks,
    -V
    You could have an argument in common_func that gets passed different values in func1 and func2:
    [code=python]
    def common_func(cal l_func):
    if call_func == 1:
    # called from func1
    if call_func == 2:
    # called from func2

    def func1():
    common_func(1)

    def func2():
    common_func(2)
    [/code]
    Why do you need that?

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by indiarocks
      Let's say for eg. I have one common function say 'common_func' and it can be called by any other functions. If func1, makes a call to common_func, I need to determine in common_func that this call was made by func1. If func2 makes a call to common_func, that this call was made by func2.

      I need a way to figure out which function made a function call to common_func.

      I don't know if I am making sense.

      Thanks,
      -V
      If you want to do that without adding parameters to the function parameter list, you'll want to use the introspection provided my the inspect module:[CODE=python]import inspect

      class FunctionOwner:
      def __init__(self):
      self.caller1 = self.func1.im_f unc.func_code
      self.caller2 = self.func2.im_f unc.func_code

      def __del__(self):
      del self.caller1
      del self.caller2

      def common_func(sel f):
      caller = inspect.current frame().f_back. f_code
      if caller == self.caller1:
      print "called by caller 1"
      elif caller == self.caller2:
      print "called by caller 2"

      def func1(self):
      self.common_fun c()

      def func2(self):
      self.common_fun c()[/CODE]
      This also applies to module functions, but I'm a class object fan and rarely define things like this at the module level.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        If you want to do that without adding parameters to the function parameter list, you'll want to use the introspection provided my the inspect module:[CODE=python]import inspect

        class FunctionOwner:
        def __init__(self):
        self.caller1 = self.func1.im_f unc.func_code
        self.caller2 = self.func2.im_f unc.func_code

        def __del__(self):
        del self.caller1
        del self.caller2

        def common_func(sel f):
        caller = inspect.current frame().f_back. f_code
        if caller == self.caller1:
        print "called by caller 1"
        elif caller == self.caller2:
        print "called by caller 2"

        def func1(self):
        self.common_fun c()

        def func2(self):
        self.common_fun c()[/CODE]
        This also applies to module functions, but I'm a class object fan and rarely define things like this at the module level.
        Well, I think it's kind of cool, even if no one else does.

        Comment

        • indiarocks
          New Member
          • Feb 2007
          • 20

          #5
          Thanks for the tip. But what if I don't know the name of function that can call common_func. It could be any arbitrary function name defined by the user who calls common_func.

          Thanks



          Originally posted by bartonc
          If you want to do that without adding parameters to the function parameter list, you'll want to use the introspection provided my the inspect module:[CODE=python]import inspect

          class FunctionOwner:
          def __init__(self):
          self.caller1 = self.func1.im_f unc.func_code
          self.caller2 = self.func2.im_f unc.func_code

          def __del__(self):
          del self.caller1
          del self.caller2

          def common_func(sel f):
          caller = inspect.current frame().f_back. f_code
          if caller == self.caller1:
          print "called by caller 1"
          elif caller == self.caller2:
          print "called by caller 2"

          def func1(self):
          self.common_fun c()

          def func2(self):
          self.common_fun c()[/CODE]
          This also applies to module functions, but I'm a class object fan and rarely define things like this at the module level.

          Comment

          • indiarocks
            New Member
            • Feb 2007
            • 20

            #6
            Was playing around with bartonc's tip and found out the answer.
            [CODE=python]
            caller = inspect.current frame().f_back. f_code
            print "I was called by " , caller.co_name
            [/CODE]

            Thanks,
            -V
            Last edited by bartonc; Jul 18 '07, 08:03 PM. Reason: Added [CODE=python][/CODE] tags.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by indiarocks
              Was playing around with bartonc's tip and found out the answer.
              [CODE=python]
              caller = inspect.current frame().f_back. f_code
              print "I was called by " , caller.co_name
              [/CODE]

              Thanks,
              -V
              Thanks. Sorry that I missed that part. Glad you found your way through the docs!

              Comment

              • MisterPete
                New Member
                • May 2007
                • 5

                #8
                Is there a way to determine the calling module name as well? Ie, I want to print "called by MyModule.SubMod ule.Foo()" not just "called by Foo()"
                I think I saw the file path for the module in there but I don't want to have to try to determine the submodules from the path if possible.

                Cheers,
                Pete

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by MisterPete
                  Is there a way to determine the calling module name as well? Ie, I want to print "called by MyModule.SubMod ule.Foo()" not just "called by Foo()"
                  I think I saw the file path for the module in there but I don't want to have to try to determine the submodules from the path if possible.

                  Cheers,
                  Pete
                  Hi Pete. Building on the example above:[CODE=python]#.....
                  print inspect.getfile (caller)[/CODE]

                  There are others, as well. The docs are here.

                  Have fun!

                  Comment

                  Working...