Fire Method by predefined string!
Hi people! Assume we have 2 methods, one called Fire and the other __DoSomething. I want the param which is a string to be converted, that I can fire directly a method. Is it somehow possible in python, instead of writing if else statements ???! Tamer class(object): def Fire(self,param) #possible ?! self.__param(): def _DoSomething(self): print 'I did it!' -- https://mail.python.org/mailman/listinfo/python-list
Re: Fire Method by predefined string!
On Sun, Nov 17, 2013 at 2:46 PM, Tamer Higazi th9...@googlemail.com wrote: Hi people! Assume we have 2 methods, one called Fire and the other __DoSomething. I want the param which is a string to be converted, that I can fire directly a method. Is it somehow possible in python, instead of writing if else statements ???! Tamer class(object): def Fire(self,param) #possible ?! self.__param(): def _DoSomething(self): print 'I did it!' You can use the getattr function to resolve an attribute (such as a method) on an object by name. For example: class Spam(object): def fire(self, name): method = getattr(self, name) method() Note that if the parameter is derived from untrusted user input, this can be a potential security hole, as the user can potentially name *any* attribute of the object. -- https://mail.python.org/mailman/listinfo/python-list
Re: Fire Method by predefined string!
In article mailman.2807.1384725251.18130.python-l...@python.org, Tamer Higazi th9...@googlemail.com wrote: Hi people! Assume we have 2 methods, one called Fire and the other __DoSomething. I want the param which is a string to be converted, that I can fire directly a method. Is it somehow possible in python, instead of writing if else statements ???! Tamer class(object): def Fire(self,param) #possible ?! self.__param(): def _DoSomething(self): print 'I did it!' I'm not sure why you'd want to do this, but it's certainly possible (as, I imagine it would be, in any language that has introspection). You can use getattr() to look up an attribute by name. Here's a little program which demonstrates this: class C: def Fire(self, param): print I'm Fire try: f = getattr(self, param) f() except AttributeError as ex: print == %s % ex def _DoSomething(self): print I'm _DoSomething if __name__ == '__main__': c = C() c.Fire(_DoSomething) c.Fire(blah) $ python s.py I'm Fire I'm _DoSomething I'm Fire == C instance has no attribute 'blah' One thing to be aware of is that a single underscore in front of a name is fine, but a double underscore (i.e. __DoSomething) invokes a little bit of Python Magic and will give you unexpected results. -- https://mail.python.org/mailman/listinfo/python-list
Re: Fire Method by predefined string!
On Sunday, November 17, 2013 3:46:16 PM UTC-6, Tamer Higazi wrote: class(object): def Fire(self,param) #possible ?! self.__param(): def _DoSomething(self): print 'I did it!' 1. First off your class declaration is not valid -- it needs an identifier! 2. Never start a function or method with a lowercase letter. Please read PEP8 3. I would advise using self documenting names. class Foo(object): def envokeMethodByName(self, name): ... But what if the method takes arguments? :) class Foo(object): def envokeMethodByName(self, name, *args, **kw): getattr(self, name)(*args, **kw) But what if want to restrict the methods? class Foo(object): def __init__(self): self.allowedNames = [ play, pause, eject, ] def envokeMethodByName(self, name, *args, **kw): if name not in self.allowedNames: raise DontAbuseMyInterfaceMan(!) getattr(self, name)(*args, **kw) -- https://mail.python.org/mailman/listinfo/python-list
Re: Fire Method by predefined string!
On Sunday, November 17, 2013 4:23:11 PM UTC-6, Rick Johnson wrote: 2. Never start a function or method with a lowercase letter. Please read PEP8 Urm... let me correct that: 2. Never start a function or method with a UPPERCASE letter. Initial uppercase should be reserved for class names only -- and any number of leading underscores does not affect that rule because underscores are not in the set [A-Za-z]! You don't want these: def __IllegalName def _IllegalName def IllegalName But these are okay: def __legalName def _legalName def legalName These are preferred, however, i detest superfluous underscores! def __legal_name def _legal_name def legal_name -- https://mail.python.org/mailman/listinfo/python-list
Re: Fire Method by predefined string!
On Sun, 17 Nov 2013 17:20:52 -0500, Roy Smith wrote: In article mailman.2807.1384725251.18130.python-l...@python.org, Tamer Higazi th9...@googlemail.com wrote: I want the param which is a string to be converted, that I can fire directly a method. Is it somehow possible in python, instead of writing if else statements ???! I'm not sure why you'd want to do this, but it's certainly possible It is very good for implementing the Command Dispatch pattern, which in turn is very good for building little command interpreters or mini- shells. Python even comes with a battery for that: import cmd import sys class MyShell(cmd.Cmd): # Override default behaviour of empty lines. def emptyline(self): pass # Define commands for our shell by prefixing them with do_. def do_hello(self, person): if person: print(Hello, %s! % person) else: print(Hello!) def do_echo(self, line): print(line) def do_double(self, num): print(2*float(num)) def do_bye(self, line): return True MyShell().cmdloop() This defines and runs a command interpreter that understands commands bye, double, echo, hello and help. (Help is predefined for you.) See also http://drunkenpython.org/dispatcher-pattern-safety.html for another use of command dispatch. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Fire Method by predefined string!
On 18/11/2013 01:41, Steven D'Aprano wrote: On Sun, 17 Nov 2013 17:20:52 -0500, Roy Smith wrote: In article mailman.2807.1384725251.18130.python-l...@python.org, Tamer Higazi th9...@googlemail.com wrote: I want the param which is a string to be converted, that I can fire directly a method. Is it somehow possible in python, instead of writing if else statements ???! I'm not sure why you'd want to do this, but it's certainly possible It is very good for implementing the Command Dispatch pattern, which in turn is very good for building little command interpreters or mini- shells. Python even comes with a battery for that: import cmd import sys class MyShell(cmd.Cmd): # Override default behaviour of empty lines. def emptyline(self): pass # Define commands for our shell by prefixing them with do_. def do_hello(self, person): if person: print(Hello, %s! % person) else: print(Hello!) def do_echo(self, line): print(line) def do_double(self, num): print(2*float(num)) def do_bye(self, line): return True MyShell().cmdloop() This defines and runs a command interpreter that understands commands bye, double, echo, hello and help. (Help is predefined for you.) See also http://drunkenpython.org/dispatcher-pattern-safety.html for another use of command dispatch. Neat, and yet another Python site to add to my list to keep an eye on, thanks. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list