LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 01: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, 01: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, 03:07 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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, 07:26 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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 07:27 PM.
 
Old 12-30-2006, 03: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, 07:45 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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



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 07:02 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM
python problem kenji Linux - Software 1 03-25-2005 12:44 PM
Python problem cdk SUSE / openSUSE 1 11-06-2004 08:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 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
Open Source Consulting | Domain Registration