Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59801 > unrolled thread
| Started by | Tamer Higazi <th982a@googlemail.com> |
|---|---|
| First post | 2013-11-17 22:46 +0100 |
| Last post | 2013-11-17 14:38 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Fire Method by predefined string! Tamer Higazi <th982a@googlemail.com> - 2013-11-17 22:46 +0100
Re: Fire Method by predefined string! Roy Smith <roy@panix.com> - 2013-11-17 17:20 -0500
Re: Fire Method by predefined string! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-18 01:41 +0000
Re: Fire Method by predefined string! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-18 02:29 +0000
Re: Fire Method by predefined string! Rick Johnson <rantingrickjohnson@gmail.com> - 2013-11-17 14:23 -0800
Re: Fire Method by predefined string! Rick Johnson <rantingrickjohnson@gmail.com> - 2013-11-17 14:38 -0800
| From | Tamer Higazi <th982a@googlemail.com> |
|---|---|
| Date | 2013-11-17 22:46 +0100 |
| Subject | Fire Method by predefined string! |
| Message-ID | <mailman.2807.1384725251.18130.python-list@python.org> |
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!'
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-17 17:20 -0500 |
| Message-ID | <roy-53F741.17205217112013@news.panix.com> |
| In reply to | #59801 |
In article <mailman.2807.1384725251.18130.python-list@python.org>,
Tamer Higazi <th982a@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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-18 01:41 +0000 |
| Message-ID | <5289704c$0$29975$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #59804 |
On Sun, 17 Nov 2013 17:20:52 -0500, Roy Smith wrote:
> In article <mailman.2807.1384725251.18130.python-list@python.org>,
> Tamer Higazi <th982a@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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-18 02:29 +0000 |
| Message-ID | <mailman.2816.1384741808.18130.python-list@python.org> |
| In reply to | #59821 |
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-list@python.org>,
>> Tamer Higazi <th982a@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
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-11-17 14:23 -0800 |
| Message-ID | <7783b587-cbab-4584-8259-7144fd5c3d11@googlegroups.com> |
| In reply to | #59801 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-11-17 14:38 -0800 |
| Message-ID | <046393db-c4d4-4fa1-85b2-a766ed555102@googlegroups.com> |
| In reply to | #59806 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web