[Tutor] importing variables

Alan Gauld alan.gauld at btinternet.com
Thu Nov 12 15:38:12 CET 2009


"Stefan Lesicnik" <stefan at lsd.co.za> wrote

> features file contains
> rt='''text'''
> 
> import features
> 
> a = 'rt'
> print features.rt  #this works
> print features.a  #this fails

Because you defined a in your current file.
You need to define it in features.

> I need to use features.a as i am iterating through a list and a would
> be the different features i want to check if they exist, and then use
> the variable.

So you need to define a in features.
Note that if you define a in features to be an empty list you can 
add/delete items to that list from the importing module.

########## features.py##########
a = []

###########main.py##########

import features
print features.a

features.a.append(66)
print features.a

features.a.remove(66)
print features.a


Does that help?

NB. I don't really recommend this approach, but it is a way 
to do what I think you want...

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list