Import Question ..

James Henderson james at logicalprogression.net
Mon Jul 12 08:50:17 EDT 2004


On Monday 12 July 2004 1:32 pm, Peter Otten wrote:
> Skulled2003 at netscape.net wrote:
> > import def
> >
> > def dosomething(something)
> >
> >    if something == 'firstlist':
> >       listdef = def.firstlistCommonPart
> >    elif something == 'secondlist':
> >       listdef = def.secondlistCommonPart
> >
> > This is quite frustrating if i have lots of definitions. So is there a
> > way where i could get something like:
>
> Now 'def' is a _really_ bad name for a module - I'll use 'module' instead.
>
> import module
>
> def dosomething(something):
>     listdef = getattr(module, something + "CommonPart")
>
> Peter

Yes, "getattr" is the answer.  This came while I was replying so I'll just add 
that "str" is also a bad name for a variable (it was in the OP), since it 
masks a built in name.  "def" is in fact an impossible name for a module: 
"import def" is a syntax error (as is "def.firstlistCommonPart") because 
"def" is a reserved keyword.

James
-- 
James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net




More information about the Python-list mailing list