Changing attributes in different objects First some background - I am developing a Python program which has thousands of lines spread across many files. I have intermediate programming skills - and no commercial OOP experience, and have taught myself Python recently. I have come across a problem which I can solve, in a number of different ways, but I'm not really happy with any of my solutions - they don't feel quite right from an OOP point of view. I have tried to make things generic so they can be used for lots of different options, but by doing so it seems massively convoluted.

I have distilled the problem down into a comparatively small piece of code for review. I have a main object, and I have an object which holds some options (which are just true/false for now, but could be other things in the future). I then have a third object (a tkinter button) that each show the status of the option (via being sunken or raised), and when clicked, change the value.

I felt the right way to do this would be for the functionality (in italics above) to be contained entirely in the toggleButton class. At first I just passed it self.options.Option1 (or whatever), but that didn't work. I then read up on does Python pass by value or reference, which (I know I am simplifying here) seemed to say that when you pass an object it passes the object, but when you pass something like an integer or boolean it passes the value. So I changed it to what you see now - where you pass the variable, an integer representing the variable, and changing the options class to have a function to change the option. It all seems so needlessly verbose. Is there a better way?

from Tkinter import *

class optionsClass():
    _OPTION1 = 1
    _OPTION2 = 2

    def __init__(self):
        self.Option1 = True
        self.Option2 = False

    def change(self, optionid):
        if optionid == optionsClass._OPTION1:
            if self.Option1 == True:
                self.Option1 = False
            else:
                self.Option1 = True
        if optionid == optionsClass._OPTION2:
            if self.Option2 == True:
                self.Option2 = False
            else:
                self.Option2 = True

class toggleButton(Button):
    def __init__(self, master, targetObject, targetoptionid, initialval
                    , **kwargs):
        Button.__init__(self, master, command = self.callback, **kwargs)
        self.targetoptionid = targetoptionid
        self.targetObject = targetObject
        if initialval==True:
            self.config(relief=SUNKEN)
        else:
            self.config(relief=RAISED)

    def callback(self):
        self.targetObject.change(self.targetoptionid)
        if self.cget("relief") == SUNKEN:
            self.config(relief=RAISED)
        else:
            self.config(relief=SUNKEN)


class mainframe(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.options = optionsClass()
        option1box = toggleButton(self, self.options, optionsClass._OPTION1,
                self.options.Option1, text="Option 1")
        option1box.grid(column=0, row=0)
        option2box = toggleButton(self, self.options, optionsClass._OPTION2,
                self.options.Option2, text="Option 2")
        option2box.grid(column=0, row=1)
        testbutton= Button(self, command = self.outputoptions,
                             text="Return option values")
        testbutton.grid(column=0, row=2)

    def outputoptions(self):
        print "Option 1 is " + str(self.options.Option1)
        print "Option 2 is " + str(self.options.Option2)


top = Tk()
m = mainframe(top)
m.pack()
top.mainloop()


Things to bear in mind...


I would like the options class to work for things other than boolean without much difficulty.
I like the convenience and readability of the main class to simply say self.options.Option1.
Ideally there will still be three classes, as mainframe and option do a lot more in the actual program, and toggleButton is used multiple times and is therefore a good candidate to be a class.
First, to answer your question on passing by value/reference, Python doesn't pass mutable (e.g. list, dict) and immutable (e.g. str, tuple) objects any differently - the difference appears because they are[n't] mutable, i.e. can[not] be changed in-place. Names in Python are just references to objects. 

When you pass an immutable object:

def calling():
    foo = "bar" # bind str object to name 'foo' in scope of 'calling'
    called(foo) # pass reference to str object to 'called'
    print(foo) # still "bar"

def called(baz): # first arg object will be bound to name 'baz' in scope of 'called'
    baz = "{0}!".format(baz) # rebinds name 'baz' to **new str object**
    print(baz)


In this case, the string is immutable, it can't be changed in-place, so changes in called won't be reflected in calling, as calling still references the original object. 

By contrast, with a mutable type:

def calling():
    foo = [] # bind list object to name 'foo' in scope of calling function
    called(foo) # pass reference to list object to 'called'
    print(foo) # see change from 'called'

def called(baz): # first arg object will be bound to name 'baz' in scope of 'called'
    baz.append("hello, world") # object bound to name 'baz' (and 'foo') **is changed**
    print(baz)




You should read and consider following the Python Style Guide, PEP-0008 - class names should be CapitalizedWords and variable, function and method names should be lowercase_with_underscores.



optionsClass can be streamlined. At the very least, note that

if self.Option1 == True:
    self.Option1 = False
else:
    self.Option1 = True


can be simplified to:

self.Option1 = not self.Option1


Dictionaries would allow you to simplify further:

class OptionsClass():

    id_map = {'Option1': 1, 'Option2': 2}

    def __init__(self, options=None):
        if options is None:
            options = {1: True, 2: False}
        self.options = options

    def change(self, option_id):
        self.options[option_id] = not self.options[option_id]

    def __getattr__(self, attr_name):
        return self.options[id_map[attr_name]]


Now you can use it:

&gt;&gt;&gt; o = OptionsClass()
&gt;&gt;&gt; o.Option1
True
&gt;&gt;&gt; o.Option2
False
&gt;&gt;&gt; o.change(1)
&gt;&gt;&gt; o.Option1
False