Perl __DATA__ construct.

Miki Tebeka miki.tebeka at gmail.com
Mon Jun 25 17:50:03 EDT 2012


> Is there a way to do the same thing in Python?
Not without some clever tricks. Either you store data at the beginning of the file or you have another file with the data.

If you really need one file and data at the end, you can roll your own. Something like:
    def data():
        with open(__file__) as fo:
            for line in fo:
                if line.startswith('# __data__'):
                    return ''.join(line[2:] for line in fo)

    def main():
        print(data())


    if __name__ == '__main__':
        main()

    # __data__
    # blah
    # blah
    # blah



More information about the Python-list mailing list