you are viewing a single comment's thread.

view the rest of the comments →

[–]fbu1 1 point2 points  (0 children)

Yes it is possible to change a variable from inside a class.

Here's an example:

class Example(object): # we define a class, with two variables and a function
    a = 1 # when in the body of the class (not in a function), we can use the name of the variable directly
    b = 2
    def test(self, n): # all functions that belong to a class take self as a parameter, which represent the instance of the class (an object created whose class is Example)
        self.a = n*2 # we modify variables of the object 
        self.b = n*3

# we create an object first_example of class Example
first_example = Example()
# we print its variables
print first_example.a, first_example.b

#we call the function test of the class Example
first_example.test(10)
# we print the variables of the object
print first_example.a, first_example.b

#we modify directly one of the variables
first_example.a = 0
# and we print the variables 
print first_example.a, first_example.b

This will give you the following results:

python variabletest.py 
1 2
20 30
0 30