[FRPythoneers] Porting perl to python

Jake Edge jake at edge2.net
Fri Jul 11 11:44:33 MDT 2003


On Fri, Jul 11, 2003 at 11:32:15AM -0600, Erick Bodine wrote:
> I have tried various manipulations using the dir() and callable()
> builtins to no avail.  I think that 'recipe' 15.3 in the Python cookbook
> might contain some clues (some manipulation around __import__) but I am
> not really familiar w/ how this works in Python.
> 
> Any help would be appreciated.
> 
> # Perl code
> my (@tests) = qw(domain_administrator
> 		 scrubber_registry_size
> 		 pr2051);
> 
> foreach my $test_name ( @tests ) {
>   print "$test_name ", ("." x (26 - length($test_name))), " ";
>   my ($rc, $reason) = &$test_name;
> }
> 		 
> # Python code
> def_list = ['test1', 'test2', 'test3]
> for f in def_list:
> 	f()			# This won't work because f is not
> callable

Hi Erick,

Perhaps instead of strings, you just want to put function references in the
list as the following illustrates:

def foo(bar):
    print bar

def bar(xyz):
    print xyz

fn_list = [ foo, bar ]
for i in fn_list:
    i(42);

would that work for you?

jake

-- 
Jake Edge - jake at edge2.net



More information about the FRPythoneers mailing list