1

I would like to know how to call one global variable with two different values in a class and call them in the other class (within which behave such as flags).

in SerialP.py

Class SerialP(object):
    def ReceiveFrame (self, data, length):
        global myvariable

        if x:
            myvariable = 1:
        elif y:
            myvariable = 2

in fmMain.py

Class fmMain:
    def OnReadConfig(self, event):
        if SerialP.myvariable = 1:
            #do this task
        if SerialP.myvariable = 2:
            #do another task
zondo
  • 19,901
  • 8
  • 44
  • 83
J2015
  • 320
  • 4
  • 24
  • 1
    Equality check is with `==`, not with `=`. Apart from that (and messy indentation), your code looks okay. What problems are you experiencing? Of course you need to call that method to create the variable first... – tobias_k Feb 25 '16 at 17:09
  • Possible duplicate of [Python - How to make a local variable (inside a function) global](http://stackoverflow.com/questions/14051916/python-how-to-make-a-local-variable-inside-a-function-global) – idjaw Feb 25 '16 at 17:09
  • The indentation of your code is incorrect. – Bryan Oakley Feb 25 '16 at 17:10

1 Answers1

1

There are a few issues with your code.
First, comparison is done with == and not with = which is used for assignment. Also, you have not included the import statement which might be misleading.

In fmMain.py

import SerialP  # and not from SerialP import SerialP

Class fmMain:
    def OnReadConfig(self, event):
      if SerialP.myvariable == 1:  # changed to ==
          #do this task
      if SerialP.myvariable == 2:  # changed to ==
          #do another task
Forge
  • 6,538
  • 6
  • 44
  • 64