1

Is there some way to return all the self variables that have been modified by a method in python?

For example, I have a method that modifies twenty self variables and want to return self.variable1, self.variable2... at the end of the method.

Here's what I'm doing now (last part of a much larger method):

return (self.p1_error, self.p1,
        self.p2_error, self.p2,
        self.p3_error, self.p3,
        self.p4_error, self.p4,
        self.p5_error, self.p5,
        self.p6_error, self.p6,
        self.p7_error, self.p7,
        self.p8_error, self.p8,
        self.p9_error, self.p9,
        self.p10_error, self.p10)

Is there a more concise way?

3
  • 1
    Why are these variables not elements of a list?
    – Blender
    Commented Jul 31, 2014 at 5:05
  • 1
    So this is part of a method on an object, and the object has dozens and dozens of instance variables, but the method only modifies 20 of the instance variables and you want to know how to detect only the modified ones? Do you know beforehand which 20 will be modified or are you asking us how to detect which ones have changed?
    – Ray Toal
    Commented Jul 31, 2014 at 5:11
  • @RayToal Yep, there are many instance variables and I just want to return the modified ones. I will know which these are but was just curious if there is a simple way of doing this without typing them all out like above.
    – TimStuart
    Commented Jul 31, 2014 at 6:56

2 Answers 2

3

for new-style classes, you can loop over entries in the class instance's __dict__:

class Example(object):

    def __init__(self):
        self.one = 1
        self.two = 2
        self.three = 3
        self.four = 4

    def members(self):
        all_members = self.__dict__.keys()

        return [ (item, self.__dict__[item]) for item in all_members if not item.startswith("_")]


print Example().members()
#[('four', 4), ('three', 3), ('two', 2), ('one', 1)]

Since the __dict__ is just a dictionary you can loop over it to select the items you need (in the above example I'm filtering out items starting with underscores but it could be any criterion you want). You can get the same information with the builtin command vars:

test = Example()
print vars(test):
# {'four': 4, 'three': 3, 'two': 2, 'one': 1}

and again you can select the keys you need.

You can also use the inspect module for more detailed inspection of an object:

import inspect
print inspect.getmembers(Example())
#[('__class__', <class '__main__.Example'>), ('__delattr__', <method-wrapper '__delattr__' of Example object at 0x000000000243E358>), ('__dict__', {'four': 4, 'three': 3, 'two': 2, 'one': 1}), ('__doc__', None), ('__format__', <built-in method __format__ of Example object at 0x000000000243E358>), ('__getattribute__', <method-wrapper '__getattribute__' of Example object at 0x000000000243E358>), ('__hash__', <method-wrapper '__hash__' of Example object at 0x000000000243E358>), ('__init__', <bound method Example.__init__ of <__main__.Example object at 0x000000000243E358>>), ('__module__', '__main__'), ('__new__', <built-in method __new__ of type object at 0x000000001E28F910>), ('__reduce__', <built-in method __reduce__ of Example object at 0x000000000243E358>), ('__reduce_ex__', <built-in method __reduce_ex__ of Example object at 0x000000000243E358>), ('__repr__', <method-wrapper '__repr__' of Example object at 0x000000000243E358>), ('__setattr__', <method-wrapper '__setattr__' of Example object at 0x000000000243E358>), ('__sizeof__', <built-in method __sizeof__ of Example object at 0x000000000243E358>), ('__str__', <method-wrapper '__str__' of Example object at 0x000000000243E358>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x0000000002255AC8>), ('__weakref__', None), ('four', 4), ('members', <bound method Example.members of <__main__.Example object at 0x000000000243E358>>), ('one', 1), ('three', 3), ('two', 2)]
1

Maintain a list in the method, whenever any variable is changed, append that to the list, return the list at the end of the method.

0

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.