J2015
J2015

Reputation: 330

How to define global variable with different values in python

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

Upvotes: 1

Views: 104

Answers (1)

Forge
Forge

Reputation: 6834

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

Upvotes: 1

Related Questions