Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
how to get a reference to the "__main__" module
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
 
WH  
View profile  
 More options Jun 9 2010, 1:29 am
Newsgroups: comp.lang.python
From: WH <whz...@gmail.com>
Date: Tue, 08 Jun 2010 22:29:04 -0700
Local: Wed, Jun 9 2010 1:29 am
Subject: how to get a reference to the "__main__" module
Hi,

I want to use one of two functions in a script:

def func_one(): pass
def func_two(): pass

func = getattr(x, 'func_'+number)
func()

'x' in getattr() should be a reference to the "__main__" module, right?
 How to get it?  The 'if' clause should work here.  I am just curious if
we can use the above method.

Thanks,
-WH


 
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.
Steven D'Aprano  
View profile  
 More options Jun 9 2010, 1:41 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve-REMOVE-T...@cybersource.com.au>
Date: 09 Jun 2010 05:41:03 GMT
Local: Wed, Jun 9 2010 1:41 am
Subject: Re: how to get a reference to the "__main__" module

On Tue, 08 Jun 2010 22:29:04 -0700, WH wrote:
> Hi,

> I want to use one of two functions in a script:

> def func_one(): pass
> def func_two(): pass

> func = getattr(x, 'func_'+number)
> func()

> 'x' in getattr() should be a reference to the "__main__" module, right?
>  How to get it?  

# File test.py
def func_one():
    return "one = 1"

def func_two():
    return "two = 2"

if __name__ == '__main__':
    import __main__
    func = getattr(__main__, 'func_' + 'two')
    print func()

    import test
    print getattr(test, 'func_' + 'one')()

which works, but importing yourself can be tricky. Try taking the "if
__name__" test out and running the script and see what happens.

This is probably a better way to solve your problem that doesn't rely on
the module importing itself:

def func_one():
    return "one = 1"

def func_two():
    return "two = 2"

funcs = {'one': func_one, 'two': func_two}

print funcs['one']

--
Steven


 
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.
Chris Rebert  
View profile  
 More options Jun 9 2010, 1:42 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Tue, 8 Jun 2010 22:42:10 -0700
Local: Wed, Jun 9 2010 1:42 am
Subject: Re: how to get a reference to the "__main__" module

On Tue, Jun 8, 2010 at 10:29 PM, WH <whz...@gmail.com> wrote:
> Hi,

> I want to use one of two functions in a script:

> def func_one(): pass
> def func_two(): pass

> func = getattr(x, 'func_'+number)
> func()

> 'x' in getattr() should be a reference to the "__main__" module, right?
>  How to get it?

from sys import modules
__main__ = modules["__main__"] # or call the variable whatever you want
assert __main__.__dict__ is globals() # purely for pedagogy
func = getattr(__main__, 'func_'+number)

Of course, in your particular case, the code can be simplified to
avoid getattr() altogether:

func = globals()['func_'+number]

Cheers,
Chris
--
http://blog.rebertia.com


 
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.
Aahz  
View profile  
 More options Jun 13 2010, 12:40 pm
Newsgroups: comp.lang.python
From: a...@pythoncraft.com (Aahz)
Date: 13 Jun 2010 09:40:50 -0700
Local: Sun, Jun 13 2010 12:40 pm
Subject: Re: how to get a reference to the "__main__" module

In article <hun8s4$h4...@news.albasani.net>, WH  <whz...@gmail.com> wrote:

>'x' in getattr() should be a reference to the "__main__" module, right?
>How to get it?  

Just for the record, the best way to get a reference to __main__ is to
import it:

import __main__
--
Aahz (a...@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra


 
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 »