Import Question ..
james at logicalprogression.net
Mon Jul 12 14:50:17 CEST 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")
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 Henderson, Logical Progression Ltd
More information about the Python-list