0

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!

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by elvenstruggle
0

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
>>>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.