setattr inside a module

Kay Schluehr kay.schluehr at gmx.net
Wed Mar 23 12:56:50 CET 2005


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




More information about the Python-list mailing list