Alex Durante
Alex Durante

Reputation: 71

Create object with attributes that have default values Python

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.

Upvotes: 2

Views: 5054

Answers (1)

BlivetWidget
BlivetWidget

Reputation: 11073

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.

Upvotes: 3

Related Questions