Referencing vars, methods and classes by name

S

Sagari

Greetings,

Sorry for a newbiw question: what is a most elegant and/or effective
way to reference vars, methods and classes by their names in Python?

To illustrate, PHP code:

$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?

References to online docs/articles related to the subject are very
welcome.

Thank you!

Konstantin
 
S

Sagari

Quite forgot to add the obvious example (in PHP):

$a = 'b';
$obj =& new $a(); // instantiating class b()
 
P

Paul Rubin

Sagari said:
$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.
 
P

Paul Rubin

Sagari said:
$a = 'b';
$obj =& new $a(); // instantiating class b()

Classes are first class objects in python:

class b: ..... # define class b

We could assign the class object to a variable

a = b

and make an instance:

obj = a() # same as "obj = b()"

Continuing that example we could make a dispatch table of classes:

class b: ...
class c: ...
class d: ...

table = [b, c, d] # 3 element array, each element is a class

i = 1
a = table # this means a = c

obj = a() # instantiate c
 
G

Gabriel Genellina

En Thu, 08 Feb 2007 05:29:23 -0300, Paul Rubin
For your last example we could say

obj.getattr(a)()

but even that is a bit ugly, depending.

Surely you meant to say getattr(obj, a)()
 
P

Paul Rubin

Gabriel Genellina said:
Surely you meant to say getattr(obj, a)()

Yeah, darn. Counterintuitive. I keep making that error in my own
code too. Maybe I should put in an RFE.
 
G

Gabriel Genellina

Yeah, darn. Counterintuitive. I keep making that error in my own
code too. Maybe I should put in an RFE.

The "method" way is using __getattribute__ or __getattr__.
A generic function helps on using it on objects of any kind - like
len()
Perhaps it was more important with old style classes.
 
G

greg

Gabriel said:
A generic function helps on using it on objects of any kind - like
len()
Perhaps it was more important with old style classes.

It also avoids intruding on the method namespace of the
object. That's important -- I like the way that the
namespace of a brand-new class is a blank slate, apart
from the double-underscore names.
 
S

Sagari

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.

Thanks to everyone for all the comments. I am migrating from PHP to
Python and I am looking for the means to port a controller code that
would, roughly speaking, call a certain method of a certain class
(both class and method names taken from user input). Putting aside
input verification (by the moment I perform the call input must have
been verified), what would be the recommended way of doing the trick?

Thanks!

All the best,

Konstantin
 
T

Terry Reedy

| Thanks to everyone for all the comments. I am migrating from PHP to
| Python and I am looking for the means to port a controller code that
| would, roughly speaking, call a certain method of a certain class
| (both class and method names taken from user input). Putting aside
| input verification (by the moment I perform the call input must have
| been verified), what would be the recommended way of doing the trick?

Use getattr(ob, name).
Suppose
mod = <module with classes, imported as 'mod'>
cname = <class name from user>
mname = <method name from user>

Then
classobj = getattr(mod, cname)
methobj = getattr(classobj, mname)

However: Python generally uses methods for instance methods. Would you be
creating an instance of the class (easily done -- inst = classobj(args))
before calling the method? If so, call methodobj(inst, args). If not, I
wonder I you should really be using (Python) classes.

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top