LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-29-2006, 02:45 PM   #1
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Python .. problem


I fount it hard to find a good thread title. I hope the code is self explainatory:

Code:
# one.py
variable = "value"

from two import *
if __name__ == '__main__': print_value()

# two.py
def print_value():
    print variable


--
python one.py
Traceback (most recent call last):
  File "one.py", line 4, in ?
    if __name__ == '__main__': print_value()
  File "/home/sybec/test/two.py", line 2, in print_value
    print variable
NameError: global name 'variable' is not defined
 
Old 12-29-2006, 02:59 PM   #2
dombrowsky
Member
 
Registered: Dec 2005
Location: New York
Distribution: Debian/GNU
Posts: 235

Rep: Reputation: 31
I don't know python too well, but I do know that its strict about types and definitions. Far more strict than perl or php.

I assume you have to define what "variable" is somehow in two.py. Like an 'extern' in C.
 
Old 12-29-2006, 04:07 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Importing two.py into the local name space of one.py makes objects from two.py available inside one.py.

But this does not mean that it also works the other way around. two.py still doesn't know about objects from one.py (like [i]variable[i]).

It is probably possible to do with some 'python trick', but then two.py will depend on one.py. This is not what people want generally, because then doesn't make sense to have seperate modules in the first place.

Or, purhaps this was what you were looking for?
Code:
variable = "value"

from two import *
if __name__ == '__main__': print_value(variable)
Code:
# two.py
def print_value(var):
    print var
 
Old 12-29-2006, 08:26 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by introuble
I fount it hard to find a good thread title. I hope the code is self explainatory:

Code:
# one.py
variable = "value"

from two import *
if __name__ == '__main__': print_value()

# two.py
def print_value():
    print variable
The "variable" variable in print_value() goes out of scope once the function returns. use the method HKO provide and it should be fine.

Last edited by ghostdog74; 12-29-2006 at 08:27 PM.
 
Old 12-30-2006, 04:12 AM   #5
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
The *problem* is that my application has a number of "n" settings which will be used by various functions (some will use "all" the settings, some only a few, some no settings at all). Now.. assuming my settings are:

Code:
set1 = "val1"
set2 = "val2"
set3 = "val3"
...
set4 = "val4"
I don't know how "efficient" it would be to pass "n" settings via arguments to a function. Obviously, if I do this, there's no need for "variable" to be global. I could store the settings in a dictionary.. or make a class which has them as attributes, and add a few methods which manipulate these settings one way or the other. But then, I'd have only one object belonging to this class. So would it be super-flu or something?
 
Old 12-30-2006, 08:45 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by introuble
I could store the settings in a dictionary.. or make a class which has them as attributes, and add a few methods which manipulate these settings one way or the other.
Good ideas IMHO.

Quote:
Originally Posted by introuble
But then, I'd have only one object belonging to this class. So would it be super-flu or something?
No. That is just fine, if done well.
 
  


Reply

Tags
correct, programming, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 08:02 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 08:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 12:05 PM
python problem kenji Linux - Software 1 03-25-2005 01:44 PM
Python problem cdk Suse/Novell 1 11-06-2004 09:38 AM


All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration