Python Forum

Full Version: Get variable from another class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Very new to Python and programming in general. I've watched videos and read tutorials about class variables, but I can't get it through my head how to make "selection" from FileType available in coordpick:

Would appreciate some help!

import arcpy
import pythonaddins

class FileType(object):
    """Implementation for pasdamappa_addin.filetype (ComboBox)"""
    def __init__(self):
        self.items = [".tif", ".jp2", ".sid"]
        self.editable = False
        self.enabled = True

    def onSelChange(self, selection):
        coordpick.enabled = True
        print selection

class coordpick(object):
    """Implementation for pasdamappa_addin.coordpick (Tool)"""
    def __init__(self):
        self.enabled = False
        self.shape = 'NONE'  # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.

    def onMouseDownMap(self, x, y, button, shift):
        print str(x)[:3] + str(y + 10000)[:2] + selection
First of all, selection isn't in FileType. You don't save it as an instance attribute in onSelChange. You would probably want to initialize it to a null value as well:

class FileType(object):
    """Implementation for pasdamappa_addin.filetype (ComboBox)"""
    def __init__(self):
        self.items = [".tif", ".jp2", ".sid"]
        self.editable = False
        self.enabled = True
        self.selection = None   # initialize to None
 
    def onSelChange(self, selection):
        coordpick.enabled = True
        self.selection = selection   # save the value as an instance attribute.
Once you have an instance, you can get the attribute with the dot operator:

>>> ft = FileType()
>>> ft.onSelChange('spam')
>>> ft.selection
'spam'