python - Increment attributes of two class without modules? -


how make class operate without importing other modules?

>>date(2014,2,2) + delta(month=3) >>(2014, 5, 2) >> >>date(2014, 2, 2) + delta(day=3) >>(2014, 2, 5) >>date(2014, 2, 2) + delta(year=1, month=2) >>(2015, 4, 2) 

this code:

# class delta(date): #     year = date(year) #     def __init__(self,y,m,d): #         self.y = y + year #         self.m = m #         self.d = d #     def __call__(self): #         return self.y, self.m, self.d class date(object):     def __init__(self,year,month,day):         self.year = year         self.month = month         self.day = day     def __call__(self):         return self.year, self.month, self.day 

override __add__ method. in delta class give __init__ default parameters can call 1 or 2 arguments.

class delta():     def __init__(self,year=0,month=0,day=0):         self.y = year         self.m = month         self.d = day     def __call__(self):         return self.y, self.m, self.d  class date():     def __init__(self,year,month,day):         self.year = year         self.month = month         self.day = day        def __call__(self):         return self.year, self.month, self.day     def isleapyear (self):         if ((self.year % 4 == 0) , (self.year % 100 != 0)) or (self.year % 400 == 0):             return true         return false     def __add__(self,delta):         self.year=self.year+delta.y         self.month=self.month+delta.m         if self.month>12:             self.month=self.month%12             self.year+=1         self.day=self.day+delta.d         if self.isleapyear() , self.day>29:             self.day=self.day%29             self.month+=1         elif not self.isleapyear() , self.day>28:             self.day=self.day%28             self.month+=1         return self.year, self.month, self.day  print(date(2014, 2, 2) + delta(year=1, month=2)) # (2015, 4, 2)  birthdate=date(1990,1,1) currentyear=birthdate+delta(year=20,month=2,day=5) print(currentyear) # (2010, 3, 6) 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -