Is there Python equivalent to Perl BEGIN{} block?

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

    #1

    Is there Python equivalent to Perl BEGIN{} block?

    Hi all,

    The subject says pretty much all, i would very appreciate an answer. I
    tried to search the various forums and groups, but didn't find any
    specific answer...

    Thanks,
    Alex.
  • Paddy

    #2
    Re: Is there Python equivalent to Perl BEGIN{} block?

    On Mar 12, 6:19 pm, Alex <alex.pul...@gm ail.comwrote:
    Hi all,
    >
    The subject says pretty much all, i would very appreciate an answer. I
    tried to search the various forums and groups, but didn't find any
    specific answer...
    >
    Thanks,
    Alex.
    No not really.

    There are lots of other ways to structure a Python program though
    which makes its omission hardly felt.

    - Paddy.

    Comment

    • Tim Chase

      #3
      Re: Is there Python equivalent to Perl BEGIN{} block?

      The subject says pretty much all,

      Given what I understand about the BEGIN block[1], this is how
      Python works automatically:

      bash$ cat a.py
      print 'a1'
      import b
      print 'a2'

      bash$ cat b.py
      print 'b'

      bash$ python a.py
      a1
      b
      a2

      However, the first import does win and Python caches
      module-loading, so take care:

      bash$ cat x.py
      print 'one'
      import a
      print 'two'
      import b
      print 'three'

      bash$ python x.py
      one
      a1
      B
      a2
      two
      three

      -tkc

      [1]
      http://www.cs.cf.ac.uk/Dave/PERL/node133.html





      Comment

      • Steven D'Aprano

        #4
        Re: Is there Python equivalent to Perl BEGIN{} block?

        On Wed, 12 Mar 2008 11:19:05 -0700, Alex wrote:
        Hi all,
        >
        The subject says pretty much all
        Only to people who know what the Perl BEGIN{} block means.



        --
        Steven

        Comment

        • Justus Schwabedal

          #5
          Re: Is there Python equivalent to Perl BEGIN{} block?

          What do you need it for anyway? I just read about it and I think it's
          useless
          in python.

          On Mar 13, 2008, at 1:03 AM, Steven D'Aprano wrote:
          On Wed, 12 Mar 2008 11:19:05 -0700, Alex wrote:
          >
          >Hi all,
          >>
          >The subject says pretty much all
          >
          Only to people who know what the Perl BEGIN{} block means.
          >
          >
          >
          --
          Steven
          >
          --
          http://mail.python.org/mailman/listinfo/python-list

          Comment

          • Carl Banks

            #6
            Re: Is there Python equivalent to Perl BEGIN{} block?

            On Mar 12, 8:11 pm, Justus Schwabedal <justus.schwabe ...@gmx.de>
            wrote:
            What do you need it for anyway? I just read about it and I think it's
            useless
            in python.

            Perl, like Python, has a separate compilation and run times. One day,
            someone who was trying to use Perl for something asked, "You know,
            wouldn't it be neat-o if you could execute some Perl code *before* you
            compiled the script?" And so (since that's usually enough reason to
            add something to Perl) was borne the BEGIN block.

            I believe the official rationale was that they wanted to add some
            magic variables that affected Perl's compilation and couldn't do it
            without a magic block that executed at compile time.

            Python solved a similar problem by introducing __future__ imports.


            Carl Banks

            Comment

            • Jeff Schwab

              #7
              Re: Is there Python equivalent to Perl BEGIN{} block?

              Alex wrote:
              The subject says pretty much all, i would very appreciate an answer. I
              tried to search the various forums and groups, but didn't find any
              specific answer...
              I'd like an answer to this, too. In Perl, I mostly used it for
              one-liners, when a variable needed to be initialized to some non-default
              value; for example, perl -ple 'BEGIN { $sum = 42 } $sum += $_'.

              Comment

              • Paddy

                #8
                Re: Is there Python equivalent to Perl BEGIN{} block?

                On Mar 13, 1:37 am, Carl Banks <pavlovevide... @gmail.comwrote :
                On Mar 12, 8:11 pm, Justus Schwabedal <justus.schwabe ...@gmx.de>
                wrote:
                >
                What do you need it for anyway? I just read about it and I think it's
                useless
                in python.
                >
                Perl, like Python, has a separate compilation and run times. One day,
                someone who was trying to use Perl for something asked, "You know,
                wouldn't it be neat-o if you could execute some Perl code *before* you
                compiled the script?" And so (since that's usually enough reason to
                add something to Perl) was borne the BEGIN block.
                >
                I believe the official rationale was that they wanted to add some
                magic variables that affected Perl's compilation and couldn't do it
                without a magic block that executed at compile time.
                >
                Python solved a similar problem by introducing __future__ imports.
                >
                Carl Banks
                And theres me thinking it was part of Perls AWK compatibility code.

                - Paddy.

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Is there Python equivalent to Perl BEGIN{} block?

                  Alex a écrit :
                  (sni)
                  First of all thanks all for answering!
                  >
                  I have some environment check and setup in the beginning of the code.
                  I would like to move it to the end of the script.
                  Why ? (if I may ask...)
                  But I want it to
                  execute first, so the script will exit if the environment is not
                  configured properly.
                  If you want some code to execute first when the script/module is loaded,
                  then keep this code where it belongs : at the beginning of the script.

                  Comment

                  • Carl Banks

                    #10
                    Re: Is there Python equivalent to Perl BEGIN{} block?

                    On Mar 13, 7:02 am, Bruno Desthuilliers <bruno.
                    42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
                    Alex a écrit :
                    (sni)
                    >
                    First of all thanks all for answering!
                    >
                    I have some environment check and setup in the beginning of the code.
                    I would like to move it to the end of the script.
                    >
                    Why ? (if I may ask...)
                    >
                    But I want it to
                    execute first, so the script will exit if the environment is not
                    configured properly.
                    >
                    If you want some code to execute first when the script/module is loaded,
                    then keep this code where it belongs : at the beginning of the script.

                    I concur with Bruno's recommendation: stuff you want to do first
                    should come first in the script. Things like BEGIN blocks hurt
                    readability because you can't identify where execution begins without
                    reading the whole file.

                    Having said that, one thing that often happens in Python scripts is
                    that all the functions are defined first, then the script logic
                    follows. So you could put the meat of your script in a function, then
                    the "BEGIN" stuff after that functions:


                    def run_script():
                    #
                    # script contained in this long function
                    #

                    # Then test preconditions here...
                    if os.environ["HELLO"] != "WORLD":
                    sys.exit(2)

                    # Then call the run_script functions
                    run_script()


                    But having said THAT, I don't recommend you do that with
                    preconditions. If the script has a quick early exit scenario, you
                    really ought to put that near the top, before the function
                    definitions, to clearly show to a human reader what is necessary to
                    run the script.


                    Carl Banks

                    Comment

                    • Paddy

                      #11
                      Re: Is there Python equivalent to Perl BEGIN{} block?

                      On Mar 13, 7:03 pm, Jonathan Gardner <jgard...@jonat hangardner.net>
                      wrote:
                      On Mar 12, 6:37 pm, Carl Banks <pavlovevide... @gmail.comwrote :
                      <<Snip>>
                      >
                      And leave out the magical -p and -n. If you want to iterate through a
                      file, "for line in sys.stdin:".
                      Or better still:

                      import fileinput
                      for line in fileinput.input ():
                      process(line)

                      - Paddy.

                      Comment

                      • Jeff Schwab

                        #12
                        Re: Is there Python equivalent to Perl BEGIN{} block?

                        Paddy wrote:
                        On Mar 13, 7:03 pm, Jonathan Gardner <jgard...@jonat hangardner.net>
                        wrote:
                        >On Mar 12, 6:37 pm, Carl Banks <pavlovevide... @gmail.comwrote :
                        >
                        <<Snip>>
                        >And leave out the magical -p and -n. If you want to iterate through a
                        >file, "for line in sys.stdin:".
                        >
                        Or better still:
                        >
                        import fileinput
                        for line in fileinput.input ():
                        process(line)
                        I write maybe a dozen one-liners a day. It's not practical to do this
                        with Python, or at least it's not nearly as convenient as Perl. I'm
                        fine with the magic. Abusing the magic is another story; Perl language
                        features are meant to be used in particular contexts, and BEGIN blocks
                        are rarely if ever necessary in Perl modules.

                        Comment

                        • Aahz

                          #13
                          Re: Is there Python equivalent to Perl BEGIN{} block?

                          In article <hMWdnSLNjsLJH0 TanZ2dnUVZ_qDin Z2d@comcast.com >,
                          Jeff Schwab <jeff@schwabcen ter.comwrote:
                          >
                          >I write maybe a dozen one-liners a day. It's not practical to do this
                          >with Python, or at least it's not nearly as convenient as Perl.
                          That is a defensible position, but my take is that writing the one-liners
                          in Python is more convenient than remembering enough Perl to make writing
                          one-liners useful. Especially when the one-liners often start expanding.
                          --
                          Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                          "All problems in computer science can be solved by another level of
                          indirection." --Butler Lampson

                          Comment

                          Working...