make order of function definitions irrelevant

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joerg Schuster

    make order of function definitions irrelevant

    Hello,

    according to



    the order of function definitions does matter in python. Does anyone
    know a trick to avoid this? Is there a way to "declare" functions
    without defining them?

    (Making the order of function definitions irrelevant would be useful
    for automatically generated python scripts.)


    Jörg


  • John Roth

    #2
    Re: make order of function definitions irrelevant


    "Joerg Schuster" <js@cis.uni-muenchen.de> wrote in message
    news:crt1xsgruu j.fsf@pinatubo. cis.uni-muenchen.de...[color=blue]
    > Hello,
    >
    > according to
    >
    > http://mail.python.org/pipermail/tut...ly/007246.html
    >
    > the order of function definitions does matter in python. Does anyone
    > know a trick to avoid this? Is there a way to "declare" functions
    > without defining them?
    >
    > (Making the order of function definitions irrelevant would be useful
    > for automatically generated python scripts.)[/color]

    What you're seeing is the order of execution as a module is
    loaded. As far as I can tell, that's not going to change, and I don't
    see any reason to change it.

    If you would tell us a bit about the application where you see
    a need for this, maybe we could make some suggestions.

    John Roth
    [color=blue]
    >
    >
    > Jörg
    >
    >[/color]


    Comment

    • Peter Otten

      #3
      Re: make order of function definitions irrelevant

      Joerg Schuster wrote:
      [color=blue]
      > according to
      >
      > http://mail.python.org/pipermail/tut...ly/007246.html
      >
      > the order of function definitions does matter in python. Does anyone
      > know a trick to avoid this? Is there a way to "declare" functions
      > without defining them?
      >
      > (Making the order of function definitions irrelevant would be useful
      > for automatically generated python scripts.)[/color]

      You cannot *call* a function before it is defined:

      fun()
      def fun(): pass

      will choke. But there is no problem with

      def first(): second()
      def second(): pass

      So I cannot see where this could be an obstacle to script generation.
      Could you provide an example?

      Peter

      Comment

      • Peter Hansen

        #4
        Re: make order of function definitions irrelevant

        Joerg Schuster wrote:[color=blue]
        >
        > according to
        >
        > http://mail.python.org/pipermail/tut...ly/007246.html
        >
        > the order of function definitions does matter in python. Does anyone
        > know a trick to avoid this?[/color]

        No.

        [color=blue]
        > Is there a way to "declare" functions without defining them?[/color]

        No.
        [color=blue]
        > (Making the order of function definitions irrelevant would be useful
        > for automatically generated python scripts.)[/color]

        No, it would not.

        Basically, the problem you believe is here is not a problem
        in actual practice. You could add declarations to Python-the-language,
        but they're irrelevant until the function is actually called.
        When the function is called, it has to have been defined (not just
        declared) and so the declaration would be completely redundant.

        -Peter

        Comment

        • anton muhin

          #5
          Re: make order of function definitions irrelevant

          Joerg Schuster wrote:
          [color=blue]
          > Hello,
          >
          > according to
          >
          > http://mail.python.org/pipermail/tut...ly/007246.html
          >
          > the order of function definitions does matter in python. Does anyone
          > know a trick to avoid this? Is there a way to "declare" functions
          > without defining them?
          >
          > (Making the order of function definitions irrelevant would be useful
          > for automatically generated python scripts.)
          >
          >
          > Jörg
          >
          >[/color]
          As was said, you usually don't need such a thing. If you desperatly
          looking for it, something like this might work:

          func = None

          def caller():
          assert func, 'func is None'
          return func()

          def foo():
          return 'foo'

          def bar():
          return 'bar'

          func = foo

          print func()

          Of course, in this case you'd better pass an actual function as a
          parameter. But, again, almost for sureit's a flaw in your design.

          regards,
          anton.

          Comment

          • Peter Hansen

            #6
            Re: make order of function definitions irrelevant

            anton muhin wrote:[color=blue]
            >
            > As was said, you usually don't need such a thing. If you desperatly
            > looking for it, something like this might work:
            >
            > func = None
            >
            > def caller():
            > assert func, 'func is None'
            > return func()
            >
            > def foo():
            > return 'foo'
            >
            > def bar():
            > return 'bar'
            >
            > func = foo
            >
            > print func()
            >
            > Of course, in this case you'd better pass an actual function as a
            > parameter. But, again, almost for sureit's a flaw in your design.[/color]

            As it stands, the first line in the above code is still redundant,
            and can be removed with no ill effects, as "func" is not actually
            called until it is bound to a real function.

            -Peter

            Comment

            • anton muhin

              #7
              Re: make order of function definitions irrelevant

              Peter Hansen wrote:
              [color=blue]
              > anton muhin wrote:
              >[color=green]
              >> As was said, you usually don't need such a thing. If you desperatly
              >>looking for it, something like this might work:
              >>
              >>func = None
              >>
              >>def caller():
              >> assert func, 'func is None'
              >> return func()
              >>
              >>def foo():
              >> return 'foo'
              >>
              >>def bar():
              >> return 'bar'
              >>
              >>func = foo
              >>
              >>print func()
              >>
              >>Of course, in this case you'd better pass an actual function as a
              >>parameter. But, again, almost for sureit's a flaw in your design.[/color]
              >
              >
              > As it stands, the first line in the above code is still redundant,
              > and can be removed with no ill effects, as "func" is not actually
              > called until it is bound to a real function.
              >
              > -Peter[/color]

              Sure, thanks. It just a little bit clearer.

              best regards,
              anton.

              Comment

              • Peter Hansen

                #8
                Re: make order of function definitions irrelevant

                anton muhin wrote:[color=blue]
                >
                > Peter Hansen wrote:[color=green]
                > > As it stands, the first line in the above code is still redundant,
                > > and can be removed with no ill effects, as "func" is not actually
                > > called until it is bound to a real function.[/color]
                >
                > Sure, thanks. It just a little bit clearer.[/color]

                I would respectfully claim that, to a Python programmer, it's actually
                just a little bit _less_ clear, just because of the "unnecessit y" of
                doing it. One might have to waste a moment to try to figure out why
                it's done that way.

                Only to a programmer coming from another language, one which requires
                such declarations, might this seem clearer, and I'm unsure about that.

                (And the sooner said programmer unlearns some things, the sooner Python
                will feel more comfortable.)

                All IMHO... no offense intended.

                -Peter

                Comment

                • John J. Lee

                  #9
                  Re: make order of function definitions irrelevant

                  Joerg Schuster <js@cis.uni-muenchen.de> writes:
                  [...][color=blue]
                  > the order of function definitions does matter in python. Does anyone
                  > know a trick to avoid this? Is there a way to "declare" functions
                  > without defining them?
                  >
                  > (Making the order of function definitions irrelevant would be useful
                  > for automatically generated python scripts.)[/color]

                  Aside from what everybody has already said, generating Python code is
                  almost always a "don't do that" anyway.

                  Most of the time, it's much better do use Python's dynamic features
                  instead of code generation.


                  John

                  Comment

                  • Joerg Schuster

                    #10
                    Re: make order of function definitions irrelevant

                    Thank you all, so far. I had asked the question because I am writing a
                    program that translates linguistic grammars into python functions. The
                    python functions are supposed to be called by another program of mine
                    that does regular expression matching. Like so:

                    (1) Linguistic grammar

                    NP --> det AP n
                    AP --> Adv+ a

                    ( Lexicon is given. Like so:

                    Det --> ['the', 'a', ...]
                    Adv --> ['really', ...]
                    A --> ['blue', 'nice', ...]
                    N --> ['programmer', 'biologist', ...]
                    )

                    (2) Python functions

                    def NP():
                    return sequence(det(), AP(), n())

                    def AP():
                    return sequence(plus(a dv(), a())

                    (3) Matching routine "pm":

                    $ pm -pattern "NP_biologi st VP_love NP_programming-language" -file my_corpus

                    Actually, (2) is lot more complex than in the example, because it needs
                    "wildcards" that can be filled with semantic and other information by
                    pm. Yet, the user should be able to write his own grammar, therefore
                    the translation (1) -> (2). (Of course, the grammar would not be a
                    context free grammar, though it looks like one.)

                    *If* the order of function definitions did not matter, then the
                    order of the grammar rules in (1) would not matter, either.

                    Yet, I thought the translation program over, and I will probably give
                    it a new design, today.

                    Jörg

                    Comment

                    • John Roth

                      #11
                      Re: make order of function definitions irrelevant


                      "Joerg Schuster" <js@cis.uni-muenchen.de> wrote in message
                      news:crtr80fz49 w.fsf@pinatubo. cis.uni-muenchen.de...[color=blue]
                      > Thank you all, so far. I had asked the question because I am writing a
                      > program that translates linguistic grammars into python functions. The
                      > python functions are supposed to be called by another program of mine
                      > that does regular expression matching. Like so:
                      >
                      > (1) Linguistic grammar
                      >
                      > NP --> det AP n
                      > AP --> Adv+ a
                      >
                      > ( Lexicon is given. Like so:
                      >
                      > Det --> ['the', 'a', ...]
                      > Adv --> ['really', ...]
                      > A --> ['blue', 'nice', ...]
                      > N --> ['programmer', 'biologist', ...]
                      > )
                      >
                      > (2) Python functions
                      >
                      > def NP():
                      > return sequence(det(), AP(), n())
                      >
                      > def AP():
                      > return sequence(plus(a dv(), a())
                      >
                      > (3) Matching routine "pm":
                      >
                      > $ pm -pattern "NP_biologi st VP_love NP_programming-language" -file[/color]
                      my_corpus[color=blue]
                      >
                      > Actually, (2) is lot more complex than in the example, because it needs
                      > "wildcards" that can be filled with semantic and other information by
                      > pm. Yet, the user should be able to write his own grammar, therefore
                      > the translation (1) -> (2). (Of course, the grammar would not be a
                      > context free grammar, though it looks like one.)
                      >
                      > *If* the order of function definitions did not matter, then the
                      > order of the grammar rules in (1) would not matter, either.
                      >
                      > Yet, I thought the translation program over, and I will probably give
                      > it a new design, today.[/color]

                      Actually, the order doesn't matter; what matters is that the
                      function *definition* has been installed into the namespace
                      before it's invoked. The function isn't executed until it's called,
                      and it won't be called until something that *isn't* a function
                      starts the program by calling something.

                      That's why scripts always end with the lines:

                      if __name__ == "__main__"
                      doSomething()

                      where doSomething() is the first function to
                      execute in the program.

                      Everything up to that point is usually just
                      loading function and class definitions into the
                      module name space.

                      You can define functions in any order you want, as long as
                      the invocation of the whole mess is at the end of the module.

                      John Roth[color=blue]
                      >
                      > Jörg[/color]


                      Comment

                      • Joerg Schuster

                        #12
                        Re: make order of function definitions irrelevant

                        > That's why scripts always end with the lines:
                        [color=blue]
                        > if __name__ == "__main__"
                        > doSomething()[/color]

                        Thanks for pointing this out to me.

                        Jörg

                        Comment

                        • Derrick 'dman' Hudson

                          #13
                          Re: make order of function definitions irrelevant

                          Joerg Schuster <js@cis.uni-muenchen.de> wrote:
                          [color=blue]
                          > (2) Python functions
                          >
                          > def NP():
                          > return sequence(det(), AP(), n())
                          >
                          > def AP():
                          > return sequence(plus(a dv(), a())[/color]

                          As pointed out, this is perfectly valid python code because the AP
                          function (and presumably det(), adv(), n(), and a()) is defined before
                          NP() is called. The key isn't the order of the definitions in the
                          file, but the order of execution. The 'def' statement is executed
                          like any other, in python. The function doesn't exist until it is
                          defined. This is rather like C, C++, and Java, you just notice it
                          differently because python provides a way to attempt to call a
                          non-existant function. In C, etc., you must define the functions
                          before compilation will succeed and compiltion is a prereq for
                          execution.

                          HTH,
                          -D

                          --
                          \begin{humor}
                          Disclaimer:
                          If I receive a message from you, you are agreeing that:
                          1. I am by definition, "the intended recipient"
                          2. All information in the email is mine to do with as I see fit and make
                          such financial profit, political mileage, or good joke as it lends
                          itself to. In particular, I may quote it on USENET or the WWW.
                          3. I may take the contents as representing the views of your company.
                          4. This overrides any disclaimer or statement of confidentiality that may
                          be included on your message
                          \end{humor}

                          www: http://dman13.dyndns.org/~dman/ jabber: dman@dman13.dyn dns.org

                          Comment

                          • Michele Simionato

                            #14
                            Re: make order of function definitions irrelevant

                            Joerg Schuster <js@cis.uni-muenchen.de> wrote in message news:<crtr80fz4 9w.fsf@pinatubo .cis.uni-muenchen.de>...[color=blue]
                            > Thank you all, so far. I had asked the question because I am writing a
                            > program that translates linguistic grammars into python functions. The
                            > python functions are supposed to be called by another program of mine
                            > that does regular expression matching.[/color]

                            If you wrap all your functions in a class and make them staticmethods,
                            the order of definition does not matter. Is this enough for you?

                            Michele

                            Comment

                            • Joerg Schuster

                              #15
                              Re: make order of function definitions irrelevant

                              Hello Michele,

                              thanks for your hint. Yet, I have decided to give my program a new
                              design which makes some old problems disappear. (Now I am having
                              trouble with some new problems, of course.)

                              Jörg

                              Comment

                              Working...