Referencing vars, methods and classes by name

Paul Rubin http
Thu Feb 8 03:29:23 EST 2007


"Sagari" <boyandin at gmail.com> writes:
> $a = ''b';
> $$a = $something; // assign to $b
> $$a($p1); // call function b($p1)
> $obj->$a(); // call method b() of the instance $obj
> 
> What is the Python way of performing the same indirections?

We would not do that.  We don't (usually) use the interpreter symbol
table as a dictionary (what in perl would be called a hash).  Instead
we use an actual dictionary.  We might say

d = {}    # make an empty dictionary
a = 'b'
d[a] = something   # assign to d['b']
some_functab[a](p1) # look up 'b' in some function table and call it

For your last example we could say

   obj.getattr(a)()

but even that is a bit ugly, depending.

For your other examples there are gross hacks using the dictionaries
that represent the local and global symbol tables, so we translate
your examples fairly directly, but stylistically we'd usually stay
away from that kind of thing.



More information about the Python-list mailing list