Python variable between classes

I'm trying to create a character creation wizard for the game. In one class, I compute the attributes of a character. In another class, I show the user which specialties are available based on symbol attributes. However, I don’t remember how to pass variables between different classes.

Here is an example of what I have:

class BasicInfoPage(wx.wizard.WizardPageSimple):            
    def __init__(self, parent, title):
         wiz.WizardPageSimple.__init__(self, parent)
         self.next = self.prev = None
         self.sizer = makePageTitle(self, title)

                    <---snip--->

         self.intelligence = self.genAttribs()

class MOS(wx.wizard.WizardPageSimple):
     def __init__(self, parent, title):
         wiz.WizardPageSimple.__init__(self, parent)
         self.next = self.prev = None
         self.sizer = makePageTitle(self, title)
      def eligibleMOS(self, event):
          if self.intelligence >= 12: 
               self.MOS_list.append("Analyst")

The problem is that I cannot figure out how to use the "intelligence" variable from the BasicInfoPage class to the MOS class. I tried several different things from all over the Internet, but nothing works. What am I missing?

Edit I realized that after I posted this, I didn’t explain it very well. I have been trying to create a computer version of Twilight 2000 RPG since the 1980s.

wxPython ; - wxPython. , " " ( BasicInfoPage) "" . , "self.intelligence".

, , , . , , . , Intel Anaylst.

, , . , , , .

+3
5

"" "". , , .

. - . , .

, "", , .

. . . [, , , .]

( )

b= BasicInfoPage(...)

b.intelligence - b BasicInfoPage.

:

class MOS( wx.wizard.PageSimple ):
    def __init__( self, parent, title, basicInfoPage ):
        <snip>
        self.basicInfo= basicInfoPage

, MOS, self.basicInfo.intelligence, MOS , BasicInfoPage.

MOS, BasicInfoPage, .

someBasicInfoPage= BasicInfoPage( ... ) 
m= MOS( ..., someBasicInfoPage )

m someBasicInfoPage.intelligence

+6

- - , .

Model-View-Control. "" "". .

, , "" . . , .

, ,

class Character( object ):
    def __init__( self ):
        self.intelligence= 10
        <default values for all attributes.>

Character .

+3

. , . , "BasicInfoPage" .

, (BaseAttribs), . , , , :

#---Run the wizard
if __name__ == "__main__":
    app = wx.PySimpleApp()
    wizard = wiz.Wizard(None, -1, "TW2K Character Creation")
    attribs = BaseAttribs

#---Create each page
    page1 = IntroPage(wizard, "Introduction")
    page2 = BasicInfoPage(wizard, "Basic Info", attribs)
    page3 = Ethnicity(wizard, "Ethnicity")
    page4 = MOS(wizard, "Military Occupational Specialty", attribs)

, S.Lott, ( , ) ; , .

, . .

+2

, , . , - ( , , , - ), . , . MOS, BasicInfoPage, . , - , , , , , - , , . . , , .

" ", , , , , - :

class Foo(object):
    def __init__(self, var):
        self.var = var

class Bar(object):
    def do_something(self, var):
        print var*3

if __name__ == '__main__':
    f = Foo(3)
    b = Bar()
    # look, I'm using the variable from one instance in another!
    b.do_something(f.var)
+1

, : .

WizardPageSimple, , .

. :

class MOS(wiz.WizardPageSimple, wiz.IntelligenceAttributes): # Or something like that.

. , .

wiz.WizardPageSimple.__init__(self, parent)

super(MOS, self).__init__(self, parent)
0

Source: https://habr.com/ru/post/1697315/


All Articles