943,746 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 506
  • Python RSS
Dec 21st, 2008
0

Change Var. from Within a Class?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elvenstruggle is offline Offline
5 posts
since Dec 2008
Dec 21st, 2008
0

Re: Change Var. from Within a Class?

Here is one way to do it:
Python Syntax (Toggle Plain Text)
  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. >>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 21st, 2008
0

Re: Change Var. from Within a Class?

Thanks a lot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elvenstruggle is offline Offline
5 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python versions
Next Thread in Python Forum Timeline: python with html





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC