Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 3rd, 2004, 02:49 PM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 12
Importing variables into a class

The problem I am having is I want to import a file which just contains variable constants and is not a class. After I import qa_variables how do I access the variables from within the Qa_config class? With a class I import then create an instance and I am done. But with the variables file which is not a class I have nothing in instantiate. Maybe I am going about this all wrong but I'm not sure.

from qa_common import *
from qa_variables import *

class Qa_config(Qa_common):
def __init__(self):
self.common = Qa_common()

Reply With Quote
  #2  
Old June 3rd, 2004, 05:12 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
If you do

from mymodule import *

then you are copying all the variables into your current namespace. This is generally frowned apon, since it pollutes your namespace and if you then import another module that has variables/classes/whatever of the same name then they will be overwritten.

It sounds to me that what you want to do is:
import mymodule

then you can refer to the variables in it with mymodule.variablename.

Modules are not the same as classes, but are objects in their own right.

Dave - The Developers' Coach

P.S. please learn to use [ code ] tags when putting python code in your posts. Without them the formatting gets screwed up and it can become unintelligible.

Reply With Quote
  #3  
Old June 3rd, 2004, 05:54 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Just to give an example if you need even more clearing up:
Code:
# module vars.py
VERSION = "1.5"
DATADIR = "/usr/local/share/myapp"
USERCONF = "/etc/myapprc"
Code:
# module myapp.py
from vars import VERSION, DATADIR, USERCONF

print "MyApp Version: %s" % VERSION
print "MyApp Shared Data: %s" % DATADIR
print "MyApp Configuration: %s" % USERCONF

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Importing variables into a class

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap