Re: Referencing vars, methods and classes by name
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
Sagari [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | 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 -- http://mail.python.org/mailman/listinfo/python-list
Referencing vars, methods and classes by name
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
Quite forgot to add the obvious example (in PHP): $a = 'b'; $obj = new $a(); // instantiating class b() -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
Sagari [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
Sagari [EMAIL PROTECTED] writes: $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[i] # this means a = c obj = a()# instantiate c -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
En Thu, 08 Feb 2007 05:29:23 -0300, Paul Rubin http://phr.cx@NOSPAM.invalid escribió: Sagari [EMAIL PROTECTED] writes: $a = ''b'; $obj-$a(); // call method b() of the instance $obj What is the Python way of performing the same indirections? 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)() -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
Gabriel Genellina [EMAIL PROTECTED] writes: obj.getattr(a)() but even that is a bit ugly, depending. 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
On 8 feb, 05:51, Paul Rubin http://[EMAIL PROTECTED] wrote: Gabriel Genellina [EMAIL PROTECTED] writes: obj.getattr(a)() but even that is a bit ugly, depending. 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. 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. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Referencing vars, methods and classes by name
Gabriel Genellina wrote: On 8 feb, 05:51, Paul Rubin http://[EMAIL PROTECTED] wrote: Gabriel Genellina [EMAIL PROTECTED] writes: Surely you meant to say getattr(obj, a)() Yeah, darn. Counterintuitive. 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. -- Greg -- http://mail.python.org/mailman/listinfo/python-list