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
> 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
Calling class method by name passed in variable
Greetings, Can someone suggest an efficient way of calling method whose name is passed in a variable? Given something like: class X: #... def a(self): # ... def b(self): # ... #... x = X() #... v = 'a' How do I call the method of x whose name is stored in v? PHP code for this would be: class X { function a() { } } $x = new X(); $v = 'a'; $x->$v(); I need a solution for Python. Could you suggest anything? The task it to call a function whose name is taken from user-supplied input. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling class method by name passed in variable
Thanks to everyone for the advice. In fact, since all the called methods trap necessary exceptions, I see no big problems with that. With all respect, Konstantin -- http://mail.python.org/mailman/listinfo/python-list