Is there Python equivalent to Perl BEGIN{} block?

Jeff Schwab jeff at schwabcenter.com
Thu Mar 13 15:37:09 EDT 2008


Paddy wrote:
> On Mar 13, 7:03 pm, Jonathan Gardner <jgard... at jonathangardner.net>
> wrote:
>> On Mar 12, 6:37 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> 
> <<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.



More information about the Python-list mailing list