pythonic equivalent of upvar?

Mike Meyer mwm at mired.org
Tue Dec 20 20:14:07 CET 2005


mackay at aims.ac.za (David MacKay) writes:
«
> <motivation note="reading this bit is optional">
>      I am writing a command-line reader for python.
>
>      I'm trying to write something with the same brevity 
> as perl's one-liner
>
> eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
>
> and with similar functionality.

Why? Do you like to make the security hole in your code hard to find?

> <question>
>  What is the pythonic way to embed a 20-line chunk of code into a function?
> I'd love to define these 20 lines as a module, and reuse the module over and over.
> but this chunk of code needs to have access to 
> the local variables of main, whatever they are called. 

You can't do that. You don't want to do that, because it will cause
user typos to make your code break in strange and mysterious ways. Put
the variables in a dictionary, indexed by variable name.

Better yet, do what Peter suggests, and learn to use optparse.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list