2

I need create an object with attributes that have default values. This object would eventually be populated with tax data. Here is a sample of what I have so far:

class taxReturn:

def __init__(self, 
             income = ['E00200', 'E00300', 'E00400', 'E00600', 'E00650', 'E00700', 'E00800', 'E00900', 'E01000', 'E01100', 'E01200', 'E01400', 'E01500', 'E01700', 'E02000', 'E02100', 'E02300', 'E02400', 'E02500'],
             stat_adj = ['E03150', 'E03210', 'E03220', 'E03230', 'E03260', 'E03270', 'E03240', 'E03290', 'E03300', 'E03400', 'E03500'],
             AGI = 'E00100'

             self.income = income
             self.stat_adj = stat_adj
             self.AGI = AGI

I'm not sure what I can enter to produce default values, or if this form is sufficient to be populated with data.

2
  • What language is this? Commented Oct 4, 2015 at 22:47
  • 2
    This language is Python Commented Oct 4, 2015 at 22:49

1 Answer 1

3

You set default values for classes the same as you would for any other function in Python.

class Foo:
    def __init__(self, a=None, b=5):
        a = a or [1, 2, 3]
        self.a = a
        self.b = b

So when you use it you get:

>>> f = Foo()
>>> f.a
[1, 2, 3]
>>> f.b
5

By the way, never use mutables in your function header. They will not behave as you expect. Use None as shown above.

Sign up to request clarification or add additional context in comments.

2 Comments

Great thanks. So will someone with tax data be able to take this code and then populate those default values with actual data?
Yes, please play with the above code to get a feeling for how Python classes work. You can access the class members with dot notation per the usual method for any other object. f.a = [5, 4, 3, 2] will change all its values. Then you could do f.a[0] = 0 and you'll get [0, 4, 3, 2] etc.

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.