Are circular dependencies possible in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Tyler

    #1

    Are circular dependencies possible in Python?

    Like C, Python seems to insist I declare functions before calling
    them - rather than, say, scanning to the end of the current script
    when it can't immediately find what function I'm referring to.

    C lets you predeclare functions to allow for the existence of
    functions with circular dependencies.

    Does Python allow you to do something similar?

    If not how do you create functions with circular dependencies in
    Python - where function A could call function B; and function
    B could call function A - or is that not possible?
    --
    __________
    |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.
  • Dave Brueck

    #2
    Re: Are circular dependencies possible in Python?

    Tim Tyler wrote:[color=blue]
    > Like C, Python seems to insist I declare functions before calling
    > them - rather than, say, scanning to the end of the current script
    > when it can't immediately find what function I'm referring to.[/color]

    Yes and no. Yes, they have to exist before you can use them (that only makes
    sense), but no, they don't have to be placed in the same order in the source
    code like they do with C:
    [color=blue][color=green][color=darkred]
    >>> def A(count):[/color][/color][/color]
    .... print 'A', count
    .... B(count + 1)
    ....[color=blue][color=green][color=darkred]
    >>> def B(count):[/color][/color][/color]
    .... print 'B', count
    .... if count < 10:
    .... A(count + 1)
    ....[color=blue][color=green][color=darkred]
    >>> B(0)[/color][/color][/color]
    B 0
    A 1
    B 2
    ....

    See - all that matters is that they exist before you call them.

    -Dave

    Comment

    • Michael Spencer

      #3
      Re: Are circular dependencies possible in Python?

      Tim Tyler wrote:[color=blue]
      > Like C, Python seems to insist I declare functions before calling
      > them - rather than, say, scanning to the end of the current script
      > when it can't immediately find what function I'm referring to.
      >
      > C lets you predeclare functions to allow for the existence of
      > functions with circular dependencies.
      >
      > Does Python allow you to do something similar?
      >
      > If not how do you create functions with circular dependencies in
      > Python - where function A could call function B; and function
      > B could call function A - or is that not possible?[/color]
      Of course, for example:

      def A(*args):
      if len(args) > 1:
      print "Calling B%s" % repr(args[1:])
      return B(*args[1:])
      else:
      print "A Called with: %s" % args[0]

      def B(*args):
      if len(args) > 1:
      print "Calling A%s" % repr(args[1:])
      return A(*args[1:])
      else:
      print "B Called with: %s" % args[0]
      [color=blue][color=green][color=darkred]
      >>> A("Arg1","Arg2" ,"Arg3","Arg 4")[/color][/color][/color]
      Calling B('Arg2', 'Arg3', 'Arg4')
      Calling A('Arg3', 'Arg4')
      Calling B('Arg4',)
      B Called with: Arg4[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Functions have to exist before you call them, but they are not 'declared'. def
      and its enclosed suite is a statement, like print, while, etc... The def
      statement is executed to define the function. For functions defined at module
      level, this statement is executed when the module is first imported (or
      reloaded) in source-code order along with any other module-level code.

      Executing a def statement compiles (but does not execute) the body of the
      function. Each function, when def'd also gets a reference to the global scope
      in which it is defined. When you execute the function body (by calling the
      function), identifiers that are not resolved in the functions own local scope
      are looked up in this global scope.

      So in the example above, the body of A calls B. B is not defined in A's local
      scope, so it is looked up in A's globals, which is the module. As long as def
      B(... has been executed in the same module before you first call A, everything
      is fine.


      Michael

      Comment

      • Dan Sommers

        #4
        Re: Are circular dependencies possible in Python?

        On Sat, 9 Apr 2005 15:57:15 GMT,
        Tim Tyler <tim@tt1lock.or g> wrote:
        [color=blue]
        > Like C, Python seems to insist I declare functions before calling them
        > - rather than, say, scanning to the end of the current script when it
        > can't immediately find what function I'm referring to.[/color]

        Python has no such restriction.

        The only restriction is that the function be defined before it is
        called. Unlike C, function definitions in Python are executable
        statements.

        If your script looks like this:

        f( ) # call f

        def f( ) # define f
        print 'hello'

        then it will fail because f has not yet been defined before it was
        called.
        [color=blue]
        > Does Python allow you to do something similar?[/color]

        No. Python has no such thing as a function declaration.
        [color=blue]
        > If not how do you create functions with circular dependencies in
        > Python - where function A could call function B; and function
        > B could call function A - or is that not possible?[/color]

        Perhaps if you can post a minimal example that shows us what you're
        running into, someone here will know how to help.

        HTH,
        Dan

        --
        Dan Sommers
        <http://www.tombstoneze ro.net/dan/>
        μ₀ × ε₀ × c² = 1

        Comment

        • Tim Tyler

          #5
          Re: Are circular dependencies possible in Python?

          Tim Tyler <tim@tt1lock.or g> wrote or quoted:
          [color=blue]
          > Like C, Python seems to insist I declare functions before calling
          > them - rather than, say, scanning to the end of the current script
          > when it can't immediately find what function I'm referring to.
          >
          > C lets you predeclare functions to allow for the existence of
          > functions with circular dependencies.
          >
          > Does Python allow you to do something similar?
          >
          > If not how do you create functions with circular dependencies in
          > Python - where function A could call function B; and function
          > B could call function A - or is that not possible?[/color]

          Thanks guys - that's made how Python works in this area abundantly clear.
          --
          __________
          |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: Are circular dependencies possible in Python?

            In <IEosBE.GtB@bat h.ac.uk>, Tim Tyler wrote:
            [color=blue]
            > Like C, Python seems to insist I declare functions before calling
            > them - rather than, say, scanning to the end of the current script
            > when it can't immediately find what function I'm referring to.[/color]

            They don't have to be declared but to be *defined* in Python before you
            can call them. A ``def`` is executed in Python -- a function object is
            given a name. The content of a Python script is executed in the order the
            code is written.
            [color=blue]
            > C lets you predeclare functions to allow for the existence of
            > functions with circular dependencies.
            >
            > Does Python allow you to do something similar?[/color]

            Yes, just define the function before it gets actually called::

            def func_a(n):
            if n > 5:
            return
            else:
            func_b(n + 1)

            def func_b(n):
            print n
            func_a(n + 1)

            func_a(0)

            What happens here is

            1. Define function A. That function B doesn't exist by now is no problem
            because it is not called yet. There's just the instruction to call it if
            the body of function A is executed. *Then* function B has to exist.

            2. Define function B. You can swap both definitions without problems.

            3. Function A is actually called and *must* exist at this point.

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • John Machin

              #7
              Re: Are circular dependencies possible in Python?

              On Sat, 9 Apr 2005 15:57:15 GMT, Tim Tyler <tim@tt1lock.or g> wrote:
              [color=blue]
              >Like C, Python seems to insist I declare functions before calling
              >them[/color]

              One is left wondering what gave you that impression about Python.
              Nothing could be further from the truth. The only construct in Python
              that smells anything like a declaration is the wartish "global". The
              Python philosophy is that everything is dynamic. You don't muck about
              with declarations; you just get on with the job. See example below,
              where we have a list of functions, which of course all follow the same
              protocol, but what that protocol is is of no concern of the Python
              compiler.

              def nothing_to_decl are(data_fields , validation_func s, other_info):
              for k, fld in enumerate(data_ fields):
              validation_func s[k](fld, k, other_info)

              Aside: How many iterations would it take for the average C programmer
              to get the declaration for "validation_fun cs" correct?

              Others have pointed out that "def" is executed. So is "class". An OO
              example of dynamism would be a bit longer, but would involve creating
              classes on the fly and stuffing into them whatever methods are
              required for the task at hand.

              HTH,

              John

              Comment

              • Tim Tyler

                #8
                Re: Are circular dependencies possible in Python?

                Marc 'BlackJack' Rintsch <bj_666@gmx.net > wrote or quoted:[color=blue]
                > In <IEosBE.GtB@bat h.ac.uk>, Tim Tyler wrote:[/color]
                [color=blue][color=green]
                > > Like C, Python seems to insist I declare functions before calling
                > > them - rather than, say, scanning to the end of the current script
                > > when it can't immediately find what function I'm referring to.[/color]
                >
                > They don't have to be declared but to be *defined* in Python before you
                > can call them. [...][/color]

                That makes three of you who have called me on my use of "declare".

                AFAICT, I was using the standard dictionary definition of this word:



                The term "declare" doesn't have the same meaning as the term "predeclare ".
                --
                __________
                |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.

                Comment

                • John Machin

                  #9
                  Re: Are circular dependencies possible in Python?

                  On Sun, 10 Apr 2005 07:34:02 GMT, Tim Tyler <tim@tt1lock.or g> wrote:
                  [color=blue]
                  >Marc 'BlackJack' Rintsch <bj_666@gmx.net > wrote or quoted:[color=green]
                  >> In <IEosBE.GtB@bat h.ac.uk>, Tim Tyler wrote:[/color]
                  >[color=green][color=darkred]
                  >> > Like C, Python seems to insist I declare functions before calling
                  >> > them - rather than, say, scanning to the end of the current script
                  >> > when it can't immediately find what function I'm referring to.[/color]
                  >>
                  >> They don't have to be declared but to be *defined* in Python before you
                  >> can call them. [...][/color]
                  >
                  >That makes three of you who have called me on my use of "declare".
                  >
                  >AFAICT, I was using the standard dictionary definition of this word:
                  >
                  > http://dictionary.reference.com/search?q=declare
                  >
                  >The term "declare" doesn't have the same meaning as the term "predeclare ".[/color]

                  We don't need no steenking dictionaries; we're using "declare" and
                  "define" in the same manner as did K&R:

                  int foo(int bar); /* declaration */

                  int foo(int bar) { /* definition */
                  return 42 * bar;
                  }



                  Comment

                  Working...