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

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

     def name_change(self):     
          name = "elvenstruggle"[/B]


# Variable

[B]name = ""[/B]


# Call Class

[B]class = name_change()[/B]

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!

Recommended Answers

All 2 Replies

Here is one way to do it:

>>> class test(object):
... 	def change_var(self, name, value):
... 		globals()[name] = value
... 		
>>> var = 123
>>> obj = test()
>>> obj.change_var('var', 456)
>>> var
456
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.