Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59802

Re: Fire Method by predefined string!

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'attribute': 0.07; 'method.': 0.07; 'string': 0.09; 'derived': 0.09; 'input,': 0.09; 'methods,': 0.09; 'parameter': 0.09; 'statements': 0.09; 'subject:string': 0.09; 'def': 0.12; 'assume': 0.14; '*any*': 0.16; 'converted,': 0.16; 'name)': 0.16; 'name):': 0.16; 'param': 0.16; 'wrote:': 0.18; 'print': 0.22; '(such': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'object.': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'method': 0.36; 'possible': 0.36; 'nov': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'name': 0.63; 'fire': 0.65; 'potentially': 0.81; 'hole,': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=XjBrwWpbsdxVPkkDBZbZ6RBOAgmgMA9noxEYoofzf2U=; b=jtNbJmAq1m+uUEbjoUzjrKjhsojIiBnUWAEEfY8Y9koowoSwWML4VdutCjbZmqaWte 57TETHC1qNaBUuWQdL7LrZv1C2fY/uLbtrXCm1sO0N5gJ+ea/soTZE7WLabYBMLV7uv7 m6cqD5owaOtUc7ZIKgTZclYuw2MS9ZD7g/RgMotxfvq/LpF8yMYvek4Ip7KEW+hNKpuk fYpDS5XI/DtzIadda3EccnFNiCY+JmS9GkGc65o5g6x4rMt7OzyrldkVj+cfELyZNXXt vY+g2E3f4eLudwE9sf0JStG7uFtZNICvvsJBgbF647n7mwLlr2djIFE4ssmNj9TninxY rUNg==
X-Received by 10.68.211.1 with SMTP id my1mr2490913pbc.55.1384726186138; Sun, 17 Nov 2013 14:09:46 -0800 (PST)
MIME-Version 1.0
In-Reply-To <52893928.1010907@googlemail.com>
References <52893928.1010907@googlemail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 17 Nov 2013 15:09:05 -0700
Subject Re: Fire Method by predefined string!
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2808.1384726191.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384726191 news.xs4all.nl 15882 [2001:888:2000:d::a6]:54338
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59802

Show key headers only | View raw


On Sun, Nov 17, 2013 at 2:46 PM, 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!'

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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Fire Method by predefined string! Ian Kelly <ian.g.kelly@gmail.com> - 2013-11-17 15:09 -0700

csiph-web