Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
getattr on a function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mr SZ  
View profile  
 More options Apr 27, 12:30 pm
Newsgroups: comp.lang.python
From: Mr SZ <sk8in_zo...@yahoo.com.au>
Date: Mon, 27 Apr 2009 09:30:33 -0700 (PDT)
Local: Mon, Apr 27 2009 12:30 pm
Subject: getattr on a function

Hi all,

Is it possible to call functions using getattr. I have written a simple script with functions that call either SSL, TLS or plain functionality.

something like:
def func():
  ...

def funcSSL():
  ...

def funcTLS():
  ...

Now, based on my args I would like to call either one of them. In my case, I can't seem to figure out what my object would be when I call getattr(object, 'func'+<encryption>) !

" life isn't heavy enough,it flies away and floats far above action"

      Enjoy a safer web experience. Upgrade to the new Internet Explorer 8 optimised for Yahoo!7. Get it now.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Apr 27, 1:15 pm
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Mon, 27 Apr 2009 19:15:42 +0200
Local: Mon, Apr 27 2009 1:15 pm
Subject: Re: getattr on a function

From within the module:

encryption = ...
f = globals()["func" + encryption]
f(...)

In other modules, assuming the module containing the function is
called 'module':

import module

encryption = ...
f = getattr(module, "func" + encryption)
f(...)

Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Apr 27, 1:32 pm
Newsgroups: comp.lang.python
From: John Machin <sjmac...@lexicon.net>
Date: Mon, 27 Apr 2009 10:32:34 -0700 (PDT)
Local: Mon, Apr 27 2009 1:32 pm
Subject: Re: getattr on a function
On Apr 28, 2:30 am, Mr SZ <sk8in_zo...@yahoo.com.au> wrote:

> Hi all,

> Is it possible to call functions using getattr. I have written a simple script with functions that call either SSL, TLS or plain functionality.

> something like:
> def func():
>   ...

> def funcSSL():
>   ...

> def funcTLS():
>   ...

> Now, based on my args I would like to call either one of them. In my case, I can't seem to figure out what my object would be when I call getattr(object, 'func'+<encryption>) !

A function is an attribute of the module that the function is defined
in. If you don't want to hard-code the name of the module,
you can use the fact that the name __name__ is bound to the current
module, and sys.modules provides a mapping from module names to the
actual module objects.

So: getattr(foomodule, 'func' + encr)
or: getattr(sys.modules[__name__], 'func' + encr)

Or, in the same module you could have:

encrfuncdict = {
   'SSL': funcSSL,
   # etc
   }

and your call would be encrfuncdict[encryption](arg1, arg2, ...)

*AND* folk reading your code wouldn't have to write in and ask what
all that getattr() stuff was doing ;-)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Apr 27, 3:01 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Mon, 27 Apr 2009 15:01:06 -0400
Local: Mon, Apr 27 2009 3:01 pm
Subject: Re: getattr on a function

Mr SZ wrote:
> Hi all,

> Is it possible to call functions using getattr. I have written a simple script with functions that call either SSL, TLS or plain functionality.

> something like:
> def func():
>   ...

> def funcSSL():
>   ...

> def funcTLS():

funcs = {'none':func, 'SSL':funcSSL, 'TLS':funcTLS}
...
cryptfunc = funcs[ <expression yielding 'none', 'SSL', or 'TLS'> ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google