473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
Jul 18 '05 #1
5 2846
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
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

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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roman Yakovenko | last post by:
Hi. I have small problem and I don't know solution. The problem: every class imported from some module X should not allow to be modified ( modified == adding or deleting new class attributes ). ...
3
by: Eric | last post by:
Slightly off topic, i know, but here goes: I'm trying to xlate a module of mine to C++. Only problem is, it makes heavy use of "setattr". Anyone know a straightforward way to do "setattr" in C++...
4
by: Gerson Kurz | last post by:
I stumbled across this (while using my homebrewn enum class): class test: pass instance = test() setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123) I would've expected some kind of...
0
by: dataangel | last post by:
For the project I'm working on right now I'm dealing with a lot of class objects that I plan to be changing at run-time through the get/has/setattr functions. I'm wondering whether or not the...
0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
10
by: Gerard Flanagan | last post by:
Hello I have the following code: #### builder.py ######### class HtmlBuilder(object): @staticmethod def page(title=''): return HtmlPage(title)
10
by: Paulo da Silva | last post by:
Hi! In a class C, I may do setattr(C,'x',10). Is it possible to use getattr/setattr for variables not inside classes or something equivalent? I mean with the same result as exec("x=10"). ...
4
by: Rotlaus | last post by:
2 weeks ago i asked for a etended getattr() which worked really fine, but now i would love to have a extended setattr() as well. Lets assume i have some classes: class A(object): def...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.