简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM