方法一:

    1. class Date:
    2. def __init__(self, year, month, day):
    3. self.year = year
    4. self.month = month
    5. self.day = day
    6. d = Date.__new__(Date)
    7. print(d)
    8. data = {'year': 2012, 'month': 12, 'day': 21}
    9. for key, value in data.items():
    10. setattr(d, key, value)
    11. print(d.year)

    方法二:

    1. from time import localtime
    2. class Date2:
    3. def __init__(self, year, month, day):
    4. self.year = year
    5. self.month = month
    6. self.day = day
    7. @classmethod
    8. def today(cls):
    9. d = cls.__new__(cls)
    10. t = localtime()
    11. d.year = t.tm_year
    12. d.month = t.tm_mon
    13. d.day = t.tm_mday
    14. return d
    15. print(Date2.today().year)