php equivalents?

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Apr 24 11:00:32 EDT 2003


"Kevin Howe" <khowe at perfnet.ca> wrote in
news:OHSpa.8996$h%2.915957 at read1.cgocable.net: 

> I'm switching from PHP to Python, and am wondering if there are Python
> equivalents to the following two PHP functions:
> 
>> register_shutdown($func)
> 
> This is basically a "cleanup" function. Just before a script ends, it
> will invoke all functions registered using register_shutdown(). This
> can be used to close database connections, clear sessions, save data,
> etc.

Use the atexit module to register functions that are called on normal 
interpreter shutdown.

> 
>> var_export($var)
> 
> This function converts a variable to a text string of valid code which
> can be reevaluated at any time. Python has pickle/cPickle, and also
> has modules that can dump/load to XML, however, none of these create
> actual code, but instead create a formatted data string.
> 
> Any help is much appreciated.

When would it be advantageous to output executable code instead of pickling 
or marshalling an object? You wouldn't want executable code that an end 
user could modify, and if the end user can't see it why would they care 
about the format.

There is no standard way to produce executable code that will reconstruct 
arbitrary Python objects although to a certain extent the repr function 
will do that for some types (e.g. numbers, and simple tuples, lists and 
dictionaries).

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list