Python Forum
Get variable from another class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get variable from another class
#1
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
Reply
#2
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'
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Types in a class nafshar 9 6,010 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 10,871 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Calling a base class variable from an inherited class CompleteNewb 3 4,102 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Can we access instance variable of parent class in child class using inheritance akdube 3 16,907 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Class variable / instance variable ifigazsi 9 7,066 Jul-28-2020, 11:40 AM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 8,445 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  Limiting valid values for a variable in a class WJSwan 5 7,130 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  Newbie: Help with variable selection in a class Slack86 5 4,429 Apr-07-2020, 08:41 PM
Last Post: jefsummers
  Using variable from a class in another .py script keegan_010 4 5,529 Mar-25-2020, 07:47 AM
Last Post: keegan_010
  How to access class variable? instances vs class drSlump 5 5,222 Dec-11-2019, 06:26 PM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.