Import Question ..

S

Skulled2003

Hello,

I have a question on import. I have a file which contains some assorted lists, and some other definitions. But all of the list names have a common part to it and a specific part in the beginning. For example:

# file containing definitions 'def.py'

firstlistCommonPart = []

secondlistCommonPart = []


Now what i want to do is:

# some other file 'some.py'

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:

def dosomething(something):

str = something+CommonPart # something is passed as a string
listdef = def.str

basically i am asking if i can pass the string value to get an attribute from the def file. I tried doing something like in the above case, but it returns and tells me def has no attribute str.

Any help would be much appreciated.

Thanks,
Vinod


__________________________________________________________________
Switch to Netscape Internet Service.
As low as $9.95 a month -- Sign up today at http://isp.netscape.com/register

Netscape. Just the Net You Need.

New! Netscape Toolbar for Internet Explorer
Search from anywhere on the Web and block those annoying pop-ups.
Download now at http://channels.netscape.com/ns/search/install.jsp
 
P

Peter Otten

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
 
J

James Henderson

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
 
J

James Henderson


This is what Guido wrote there:

(Not that it isn't a good idea to avoid obvious clashes --
'str' for string variables and 'type' for type variables being the
most obvious stumbling blocks.)

I can accept that. :)

(Actually, in my haste I didn't notice that "str" was being defined inside a
function. As a global it really would be a no-no.)

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top