1

I have a list of settings defaults held within my init function. These defaults are all instance variables. For example,

self.set_acqmode = 1
self.set_readmode = 4
self.set_triggermode = 0

I have a function within this class which I want to use to change these default settings by only passing in a name and a value as arguments. For example,

def change_setting(self, setting, *arg):

What would be a pythonic way of accessing and changing the correct instance variable. I have tried using both vars() and dict to view these variables but the former only showed the the classes functions and the latter needs to refer to a instance of the class (which won't exist yet as this is within the class).

(If there is a better way of doing this without searching for the variables I would still like to know how to view them, just out of interest.)

1
  • To answer the second part of my own question. I found out through reading answers that I can use self as an object reference. So self.__dict__ shows all the instance variables. Commented Jan 8, 2014 at 16:16

4 Answers 4

4

Use setattr:

def change_setting(self, setting, *arg):
    setattr(self, setting, arg)
Sign up to request clarification or add additional context in comments.

Comments

1

setattr will work. But you have to ask, if you're going through all this trouble to rewrite setattr, are you even doing this the right way?

If you just want to have some arbitrary keys & values - well, that's a dictionary, and you should use it as such. override __getitem__/__setitem__ if you need custom behaviour.

if you really need attributes, then there's no reason a parent function wouldn't just do

myobj.set_triggermode = value

rather than the overly complex

myobj.change_setting('triggermode', value)

so you should do that. and even if you want to do that for some reason - use kwargs instead of args, probably closer to what you want.

3 Comments

I did think of doing a parent function for each setting but there are a lot of settings. This way I can have only one function and the it changes the variables as needed. Some of these settings won't be changed often but I still need to have a way of changing if I need to. A whole function would take ages.
No... you don't need a function. If an object has an attribute x, then you can change it by just doing obj.x = value. You don't have to write getters/setters, attributes are always public in python.
You absolutely correct. There is me over thinking things ... again. Your solution is straight forward. I did learn more though <- Me trying to justify my stupidity.
1

You can certainly use __dict__:

>>> class Test(object):
    def __init__(self):
        self.x = 1
    def set(self, attr, val):
        self.__dict__[attr] = val

>>> a = Test()
>>> a.set('x', 2)
>>> a.x
2

Comments

0

You can use __dict__ on the name of a class without having instantiated an object of that class. For example:

print myClass.__dict__.keys()

::edit:: Of course, if you're being rigorous in your programming, you may consider using __slots__, in which case you will have predefined all the instance variables of your class by yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.