By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

setattr inside a module

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 18 '05 #2

P: n/a
On Wed, 23 Mar 2005 11:35:34 +0100, kramb64 <kr*****@hotmail.com>
wrote:
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.

Jul 18 '05 #3

P: n/a

kramb64 wrote:
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

Jul 18 '05 #4

P: n/a
> I found this:
setattr(__import__(__name__), name, value)

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


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

--
Regards,

Diez B. Roggisch
Jul 18 '05 #5

P: n/a
On Wed, 23 Mar 2005 11:35:34 +0100, kramb64 wrote:
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.

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.