![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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"! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Hmm... Perhaps something like:
def set_variable(name, value, obj = None):
if obj is None:
globals()[name] = value
else:
vars(obj)[name] = value |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |