Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 11th, 2006, 6:51 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Dynamically set variables?

Say I import another Python script into the Python script I'm working on. I want to be able to set variables in that file, OR in the file I'm working in, by passing a single variable. For example:

import thing

def set_variable(cls):
      cls.new_variable = "cheese"

set_variable(thing)
set_variable(self) # This doesn't work!

print thing.new_variable
print new_variable # I want both of these to print "cheese"!
titaniumdecoy is offline   Reply With Quote
Old Jul 11th, 2006, 7:09 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Hmm... Perhaps something like:
def set_variable(name, value, obj = None):
    if obj is None:
        globals()[name] = value
    else:
        vars(obj)[name] = value
To set a variable in the local module, omit the obj argument.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
When to use the new keyword in C++? titaniumdecoy C++ 28 Mar 16th, 2006 12:36 PM
[Efficiency] Variables vs. Calculations kurt C 7 Dec 29th, 2005 2:39 PM
Dynamic memory - variables darkone916 C++ 4 Dec 7th, 2005 6:24 AM
Variables coldDeath Python 4 Aug 9th, 2005 11:35 AM
global variables uman C++ 2 Feb 20th, 2005 10:39 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC