0

I have a class, lets say:

#!/usr/bin/env python

class testclass(object):
    def __init__(self, a, b, c):
        self.__attr1 = a
        self.__attr2 = b
        self.__attr3 = c

    def meth(self):
        ## run some checks on self.__attr1, self.__attr2, self.__attr3 
                    ## How to do that?  

if __name__ == '__main__':
    t = testclass("dd", "ee", "gg")

Question: In case I have several attributes and not just three what is the best way to write the meth method ? I am going to run the same check on all attributes.

2 Answers 2

1

The easiest way is don't store them as individual attributes. In this example I'm using argument packing to pass a, b, and c as the tuple abc, and storing that as self._attrs:

class testclass(object):
    def __init__(self, *abc):
        self._attrs = abc

    def meth(self):
        for attr in self._attrs:
            print attr

if __name__ == '__main__':
    t = testclass("dd", "ee", "gg")
Sign up to request clarification or add additional context in comments.

1 Comment

Also, if you want to access all attributes to your instance, you can call dir(self), but you'll get built-ins too. Best to grab what you want, having stored appropriately
0

not very sure about what do u mean by checks on attr

but still i think

def get(self, instance, owner):

is something can help u :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.