self for modules

Discussion in 'Python' started by Bertram Scharpf, Mar 5, 2004.

  1. Hi,

    when I want to access the module itself, I can write

    ----
    import sys
    module_self = sys.modules[ __name__]
    ----

    Is there a smarter way to do this?

    On the other side, I could express my problem in the
    following way: Is it legitimate to say

    ----
    def f( d):
    d.update( { 'a': 'spam', 'b': 'eggs'})

    f( vars())

    print a, 'and', b
    ----

    ?

    Thanks for your answers in advance.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Mar 5, 2004
    #1
    1. Advertising

  2. Bertram Scharpf

    Terry Reedy Guest

    "Bertram Scharpf" <> wrote in message
    news:-scharpf...
    > Hi,
    >
    > when I want to access the module itself, I can write
    >
    > ----
    > import sys
    > module_self = sys.modules[ __name__]
    > ----
    >
    > Is there a smarter way to do this?


    shorter and not implementation specific is

    >>> mymod = __import__(__name__)
    >>> mymod

    <module '__main__' (built-in)>

    I presume this works in imported module also.

    Terry J. Reedy
     
    Terry Reedy, Mar 5, 2004
    #2
    1. Advertising

  3. Hallo,

    Terry Reedy <> schrieb:
    >
    > "Bertram Scharpf" <> wrote in message
    > news:-scharpf...
    >>
    >> when I want to access the module itself, I can write
    >>
    >> ----
    >> import sys
    >> module_self = sys.modules[ __name__]
    >> ----
    >>
    >> Is there a smarter way to do this?

    >
    > shorter and not implementation specific is
    >
    >>>> mymod = __import__(__name__)
    >>>> mymod

    ><module '__main__' (built-in)>
    >
    > I presume this works in imported module also.


    Nice idea, indeed. Thank you.

    Bertram

    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Mar 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    612
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    612
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    420
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    396
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    419
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page