setattr inside a module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kramb64

    #1

    setattr inside a module

    I'm trying to use setattr inside a module.
    From outside a module it's easy:

    import spam
    name="hello"
    value=1
    setattr(spam, name, value)

    But if I want to do this inside the module spam itself, what I've to
    pass to setattr as first argument?

    Thanks a lot for your time.
    Marco.
  • Denis S. Otkidach

    #2
    Re: setattr inside a module

    On Wed, 23 Mar 2005 11:35:34 +0100 kramb64 wrote:

    K> I'm trying to use setattr inside a module.
    K> >From outside a module it's easy:
    K>
    K> import spam
    K> name="hello"
    K> value=1
    K> setattr(spam, name, value)
    K>
    K> But if I want to do this inside the module spam itself, what I've to
    K> pass to setattr as first argument?

    globals()[name] = value

    or

    setattr(__impor t__(__name__), name, value) # note, circular import here

    --
    Denis S. Otkidach
    http://www.python.ru/ [ru]

    Comment

    • kramb64

      #3
      Re: setattr inside a module

      On Wed, 23 Mar 2005 11:35:34 +0100, kramb64 <kramb64@hotmai l.com>
      wrote:
      [color=blue]
      >I'm trying to use setattr inside a module.
      >From outside a module it's easy:
      >
      >import spam
      >name="hello"
      >value=1
      >setattr(spam , name, value)
      >
      >But if I want to do this inside the module spam itself, what I've to
      >pass to setattr as first argument?
      >
      >Thanks a lot for your time.
      >Marco.[/color]


      I found this:
      setattr(__impor t__(__name__), name, value)

      But too much underscores.... Nothing better?
      Marco.



      Comment

      • Kay Schluehr

        #4
        Re: setattr inside a module


        kramb64 wrote:[color=blue]
        > I'm trying to use setattr inside a module.
        > From outside a module it's easy:
        >
        > import spam
        > name="hello"
        > value=1
        > setattr(spam, name, value)
        >
        > But if I want to do this inside the module spam itself, what I've to
        > pass to setattr as first argument?
        >
        > Thanks a lot for your time.
        > Marco.[/color]

        ???

        Why don't You create 'name' and 'value' as module scoped variables just
        by defining them?

        If You want to introspect the module within the module, just define

        # defined within spam
        def introspect():
        import spam
        print dir(spam)

        introspect()

        Regards Kay

        Comment

        • Diez B. Roggisch

          #5
          Re: setattr inside a module

          > I found this:[color=blue]
          > setattr(__impor t__(__name__), name, value)
          >
          > But too much underscores.... Nothing better?
          > Marco.[/color]

          setattr(sys.mod ules[__name__], name, value)

          --
          Regards,

          Diez B. Roggisch

          Comment

          • Jeremy Bowers

            #6
            Re: setattr inside a module

            On Wed, 23 Mar 2005 11:35:34 +0100, kramb64 wrote:
            [color=blue]
            > I'm trying to use setattr inside a module.
            > From outside a module it's easy:
            >
            > import spam
            > name="hello"
            > value=1
            > setattr(spam, name, value)
            >
            > But if I want to do this inside the module spam itself, what I've to
            > pass to setattr as first argument?
            >
            > Thanks a lot for your time.
            > Marco.[/color]

            As others point out,

            sys.modules[__name__]

            I find myself doing this more and more, not less, as I get further into
            Python; autogenerating many similar functions, pulling constants from an
            external source, stuff like that, usually very meta and every instinct
            says its the right thing to do.

            Maybe we should have a __module__ attribute that is a reference to the
            current module? sys.modules[__name__] is a little obtuse.

            Comment

            Working...