Change Var. from Within a Class?

Thread Solved

Join Date: Dec 2008
Posts: 5
Reputation: elvenstruggle is an unknown quantity at this point 
Solved Threads: 0
elvenstruggle elvenstruggle is offline Offline
Newbie Poster

Change Var. from Within a Class?

 
0
  #1
Dec 21st, 2008
Hello, I'm new to Python and I'm using Pydev. I'm having a problem: I have several classes and several variables outside the classes. I want to change a variable outside the classes from within a class:

e.g.

# Class

class name_change(object):
     """Change a text variable outside this class"""

     def name_change(self):     
          name = "elvenstruggle"


# Variable

name = ""


# Call Class

class = name_change()


Basically I want to change the name variable through the class, and I'm having trouble. It's not working! Please help me! Thanks a lot!
Last edited by elvenstruggle; Dec 21st, 2008 at 8:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Change Var. from Within a Class?

 
0
  #2
Dec 21st, 2008
Here is one way to do it:
  1. >>> class test(object):
  2. ... def change_var(self, name, value):
  3. ... globals()[name] = value
  4. ...
  5. >>> var = 123
  6. >>> obj = test()
  7. >>> obj.change_var('var', 456)
  8. >>> var
  9. 456
  10. >>>
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: elvenstruggle is an unknown quantity at this point 
Solved Threads: 0
elvenstruggle elvenstruggle is offline Offline
Newbie Poster

Re: Change Var. from Within a Class?

 
0
  #3
Dec 21st, 2008
Thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC