setattr inside a module

K

kramb64

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.
 
K

kramb64

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.


I found this:
setattr(__import__(__name__), name, value)

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

Denis S. Otkidach

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(__import__(__name__), name, value) # note, circular import here
 
K

Kay Schluehr

kramb64 said:
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.

???

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
 
D

Diez B. Roggisch

I found this:
setattr(__import__(__name__), name, value)

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

setattr(sys.modules[__name__], name, value)
 
J

Jeremy Bowers

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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top