Firstly I don't understand english very well, so there may be a lot of mistakes.
I use python-2.6.2, wxPython2.8-win32-unicode-188.8.131.52-py26 and numpy-1.3.0-win32-superpack-python2.6 in this example.
So I would like to hnow, how to get variable elements form one class into another. I use wxPython, and i don't hnow how get value from "block" to show it into another "block". This program must be as graphical calculator with plot function.
To use a variable from one class in another class you first need to make it available for other classes to use. To do this put "self." in front of the variable. When you create an instance of the class with the variable in, you will give this instance a name. The name of the instance can then be used to access the variable in the class. For example:
class SomeClass(): def __init__(self): i = SomeClass2() #create instance of SomeClass2 called i #can now access variables in SomeClass2 by doing: print i.somevariable #this will print "4" class SomeClass2(): def __init__(self): self.somevariable = 4 #when python sees "self" it will know this variable belongs #to the class and can be obtained by replacing the "self" with #whatever you named the instance of the class #have to create instance of SomeClass: b = SomeClass()
Just to add to shibby's very nice explanation:
# passing a class variable to other classes class C1(object): def __init__(self): print("in class C1") # self makes mc1 available to the class instance self.mc1 = 77 class C2(object): mc2 = 88 def __init__(self): print("in class C2") # create an instance of C1 within C2 (shibby's example) ac1 = C1() # now access mc1 using this instance print(ac1.mc1) # 77 # show C2's own variable print(self.mc2) # 88 class C3(C1): """C3 inherits C1""" mc3 = 99 def __init__(self): print("in class C3") # make C1 part of C3's self C1.__init__(self) print(self.mc1) # 77 # show C3's own variable print(self.mc3) # 99 # create instances of class C2 and class C3 ac2 = C2() ac3 = C3() """ my display --> in class C2 in class C1 77 88 in class C3 in class C1 77 99 """